Is there an elegant way to store a dual relationship (i.e. user 1 and user 2 are friends)

前端 未结 5 1104
野趣味
野趣味 2020-12-16 06:01

I\'ve run into the same problem in two different pieces of work this month:

Version 1: User 1 & User 2 are friends
Version 2: Axis 1 & Axis 2 when gr         


        
5条回答
  •  太阳男子
    2020-12-16 07:01

    In SQL it's easy to implement the constraints to support your first approach:

    CREATE TABLE MutualFriendship
    (u1 VARCHAR(10) NOT NULL,
     u2 VARCHAR(10) NOT NULL,
     PRIMARY KEY (u1,u2),
     FOREIGN KEY (u2,u1) REFERENCES MutualFriendship (u1,u2));
    
    INSERT INTO MutualFriendship VALUES
    ('Alice','Bob'),
    ('Bob','Alice');
    

提交回复
热议问题