Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2004
    Posts
    32

    Unanswered: Enforcing a relationship

    I'd like to create a relationship between two tables in sql server 2005, where a User can be a Partner and a Partner must be a User. So it 0-1 and 1-1. How do I enforce this relationship?

    I will also be creating a view from this, will there be any problems when inserting data into the view?

    I might aswell add that I'm actually working on asp.net's new membership and roles, so I don't want to change the default tables.

  2. #2
    Join Date
    Jan 2005
    Posts
    28
    Hi,

    You can create a foreign key in the Partners table referencing the Users table.

    You can use the following script for a sample.


    CREATE TABLE [dbo].[W_Users](

    [UserId] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](50) NULL,
    [LastName] [nvarchar](50) NULL,

    CONSTRAINT [W_PK_Users] PRIMARY KEY CLUSTERED
    (
    [UserId] ASC
    )
    )



    GO

    CREATE TABLE [dbo].[W_Partners] (
    [PartnerId] [int] NOT NULL,
    [UserId] [int] NOT NULL,

    CONSTRAINT [W_PK_Partners] PRIMARY KEY CLUSTERED
    (
    [PartnerId] ASC
    )
    )

    GO
    ALTER TABLE [dbo].[W_Partners] WITH CHECK ADD CONSTRAINT [W_FK_Partners_Users] FOREIGN KEY([UserId]) REFERENCES [dbo].[W_Users] ([UserId])
    GO


    Eralper
    http://www.kodyaz.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •