views:

298

answers:

3

I asked this question previously but the answers weren't what I was looking for.

I created a table in Asp.net without using code. It contains two columns.

YourUserId and FriendUserId

This is a many to many relationship.

Heres what I want:

There can be multiple records with your name as the UserId, there can also be multiple records with FriendUserId being the same...but there cannot be multiple records with both being the same. For example:

Dave : Greg

Dave : Chris

Greg : Chris

Chris : Greg

is good

Dave : Greg

Dave : Greg

is not good.

I right clicked on the table and chose Indexes/Keys. I then put both columns in the columns section and chose to make the unique. I thought this would make them unique as a whole but individually not unique.

If you go to the Dataset, it show keys next to both columns and says that there is a constraint with both columns being checked.

Is there a way of just making sure that you are not inserting a duplicate copy of a record into the table without individual columns being unique?

I tried controling it with my sql insert statement but that did not work. This is what I tried.

INSERT INTO [FriendRequests] ([UserId], [FriendUserId]) VALUES ('"+UserId+"', '"+PossibleFriend+"') WHERE NOT EXIST (SELECT [UserId], [FriendUserId] FROM [FriendRequests])

That didn't work for some reason. Thank you for your help!

+1  A: 

Sounds like you need a composite key to make both fields a single key.

Miyagi Coder
+3  A: 

You should create a compound primary key to prevent duplicate rows.

ALTER TABLE FriendRequests
ADD CONSTRAINT pk_FriendRequests PRIMARY KEY (UserID, FriendUserID)

Or select both columns in table designer and right click to set it as a key.

To prevent self-friendship, you'd create a CHECK constraint:

ALTER TABLE FriendRequests
ADD CONSTRAINT ck_FriendRequests_NoSelfFriends CHECK (UserID <> FriendUserID)

You can add the check constraint in the designer by right clicking anywhere in the table designer, clicking "Check constraints", clicking "add", and setting expression to UserID <> FriendUserID

You might want to look at this question

Mehrdad Afshari
How do you do that without code because I did not create the tables in that fashion? Is it bad design to create the table without code?
You can still alter the table with code. But as I said, open up the table designer, select both columns and right click to set as key.
Mehrdad Afshari
Im sorry I commented before you finished typing the answer...thank you!
No problem, by the way, I updated the answer to add how to create a check constraint on a table in design view.
Mehrdad Afshari
When you say table designer do you mean in the data set file? if you mean in table definition, you can't select both tables. However I did create a constraint by right clicking on the table and choosing keys. I added both columns and set as unique, it didn't work
I mean table definition in your SQL Server database. I'm pretty sure it can be done. Right click on your table on server explorer. Edit table definition, remove any other unique constraints you might have created and use Ctrl+Click to select the two column headers, then click Set Primary Key
Mehrdad Afshari
You were right...although you can't select both columns. You right open the table definition. Then you right click just on one of the columns and click Indexes/Keys. Put both columns in column field. The set as unique.
That is actually what I had originally but I didn't realize that it was never get to my insert statement. I was trying to access one of the columns in gridview that didn't exist and it was causing the program to crash. Thanks for all your help!
A: 

I have a better idea. Create a new table. Called FriendRequestRelationships. Have the following columns

FriendRelationshipId (PRIMARY KEY) UserId_1 (FOREIGN KEY CONSTRAINT) UserId_2 (FOREIGN KEY CONSTRAINT)

Put a unique constraint to only allow one relationship wit UserId_1 and UserId_2. This table now serves as your many-to-many relationship harness.

Create a scalar function that can return the FriendUserId for a UserId, lets say it's called fn_GetFriendUserIdForUserId

You can now display your relationships by running the following query

SELECT dbo.fn_GetFriendUserIdForUserId(UserId_1) AS 'Friend1', dbo.fn_GetFriendUserIdForUserId(UserId_2) AS 'Friend2', FROM FriendRelationshipId

Danny G