MySQL friends table

后端 未结 5 1881
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 08:32

I have a MySQL DB in which I store data about each user.

I would like to add a list of friends for each user. Should I create a table of friends for each user in the

5条回答
  •  鱼传尺愫
    2020-12-08 08:47

    You are looking for M-to-N or many-to-many join table.

    Table Users:

    USER_ID  integer primary key,
    NAME     varchar
    

    Table Friendships

    USER_ID    integer not null,
    FRIEND_ID  integer not null,
    

    Both USER_ID and FRIEND_ID are foreign keys that reference Users table (Users.user_id).

    If user 123 is friend of user 921. Add row (123, 921) to Friendships table.

提交回复
热议问题