Before you show me duplicates, please note that I've searched through the site an have found some examples but not quite specific to my problem :)
What's the best way to create a Friendship table in SQL, but making sure that each row is unique in the sense that the same UserID and FriendID will never be alowed regardless of which column they belong to?
I have this rough example
CREATE TABLE [dbo].[Friendship](
[UserID] [uniqueidentifier] NOT NULL,
[FriendID] [uniqueidentifier] NOT NULL,
[FriendshipStatus] [int] NOT NULL
)
And there are 2 foreign keys to the Users table, both from UserID and FriendID.
At the moment though, I can insert a Friendship between users twice, thus creating a duplicate. Example
UserID FriendID FriendshipStatus
Guid 123 Guid 789 1
Guid 789 Guid 123 1
How do I ensure this integrity is enforced, perhaps 2 PKs? Some sort of a unique Index? Or would you suggest a better table design all together? Also, would you put an autoincrementing FriendshipID
? If so, can you explain why?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…