best way to store 1:1 user relationships in relational database

后端 未结 4 1035
独厮守ぢ
独厮守ぢ 2021-02-01 09:24

What is the best way to store user relationships, e.g. friendships, that must be bidirectional (you\'re my friend, thus I\'m your friend) in a rel. database, e.g. MYSql?

<
4条回答
  •  梦如初夏
    2021-02-01 10:12

    I would have a link table for friends, or whatever, with 2 columns both being PK's, and both being FK's to the User table.

    Both columns would be the UID, and you would have two rows per friend relationship (A,B and B,A). As long as both columns are PK's, it should still be in normal format (although others are free to correct me on this)

    Its a little more complex of a query, but nothing that can't be abstracted away by a stored procedure or some business logic, and its in Normal Format, which is usually nice to have.

提交回复
热议问题