Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3440276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:24:47+00:00 2026-05-18T08:24:47+00:00

Before you show me duplicates, please note that I’ve searched through the site an

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:24:48+00:00Added an answer on May 18, 2026 at 8:24 am

    Making the primary key for the FRIENDSHIP table to be:

    • userid
    • friendid

    …will ensure that you can’t have duplicates in order. Meaning, it will stop you from adding duplicates of userid “123” and friendid “789”. If you include the status column, that’s no longer the case, because a different status value will allow for duplicates of the userid and friendid column.

    Stopping Reverse Pairs

    In order to stop reverse pairs — userid “789” and friendid “123” — you need to either include the logic to check if the pair already exists in the table in a stored procedure, function, or trigger. A CHECK constraint of userid < friendid would stop a valid attempt to add userid “789” and friendid “123” if the reverse doesn’t already exist.

    INSERT INTO FRIENDSHIP
    SELECT @userid, @friendid, 1
      FROM FRIENDSHIP f
     WHERE NOT EXISTS(SELECT NULL
                        FROM FRIENDSHIP t
                       WHERE (t.userid = @friendid AND t.friendid = @userid)
                          OR (t.userid = @userid AND t.friendid = @friendid)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before you start pointing me to duplicates just know that I have read nearly
Before, I have found the Cost in the execution plan to be a good
Before you answer this I have never developed anything popular enough to attain high
Before I jump headlong into C#... I've always felt that C, or maybe C++,
Before you start firing at me, I'm NOT looking to do this, but someone
I'm running a master-slave MySQL binary log replication system (phew!) that, for some data,
I have an Access 2007 database that works asynchronously with a MAS200 database. Due
XML is cool in flex, but I have an annoying problem to solve, caused
Hi before going to direct problem let me show my code : //Definition of
I want to show no scope bar when the table is empty (before the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.