|
Question : SQL creating table relationship trouble
|
|
I am developing a system where Users listed in the Users Table can select a set of Users from the same table as Trusted Friends, I store these selections in a seperate TrustedUser Table with an associated status message.
(* = primary key)
Users Table -------------------------------------------- *UsersId Name 1 Bob 2 Gill 3 Fred 4 Jim (Auto-Increment)
TrustedUsers Table -------------------------------------------- *UsersID *TrustsUserID Status 1 2 AwaitingVerification 1 3 Trusted 2 1 Trusted 2 4 Blocked
The way I understand it is I need to create UsersID & TrustsUserID as a combined primary key. and then create a relationship to UsersID (in the Users table)
I imagine the relationship should be
Users Table TrustedUsers Table -------------------------------------------- UserID (1 to many)<-----> UsersID UserID (1 to many)<-----> TrustsUserID But when I try to create the relationship in MS SQL 2002 I keep getting this error:
The columns in table 'Users' do not match an existing primary key or UNIQUE constraint.
I don't understand why as I have created the primary keys?
Can anyone spot what I'm doing wrong, im sure its something intensly trivial but my mind has gone blank, cheers.
|
|
Answer : SQL creating table relationship trouble
|
|
create table Users ( UsersId int primary key , Name varchar(128) not null ) go create table TrustedUsers ( UsersID int foreign key references Users (UsersId) , TrustedUsersId int foreign key references Users (UsersId) , primary key (UsersId, TrustedUsersId) ) go
|
|
|
|