search

Home  >  Q&A  >  body text

java - 好友关系表数据的存储问题,存一条还是两条?

项目中设计好友关系,存储在一张表里面
现有用户id为2和3加为好友
好友关系表:

id user_id friend_id
1 2 3
2 3 2

这两条数据是否是一样的?还是应该存为一条数据?

后续可能还会有权限相关设计
如:

id user_id friend_id 是否允许查看自己的动态 是否查看对方的动态消息
1 2 3
2 3 2

这种情况是存在一张表里面,还是将权限和好友关系分开,单独存一张权限表

阿神阿神2888 days ago670

reply all(7)I'll reply

  • 迷茫

    迷茫2017-04-18 10:35:38

    You can save 2 entries in the friend relationship table. After all, it seems unintuitive to save one. You can save 2 entries to query the friend whose user_id is 2. select friend_id from table where user_id = 2 。 存1条的话比较麻烦些,select friend_id from table where user_id = 2 union select user_id from table where friend_id=2 Let's separate the permission tables. The relationship table maintains relationships, and the permission table maintains permissions. Don't do it together, because you may need to add other permissions in the future, and you will definitely run into trouble if you put them in one table. At least it wouldn't be that troublesome if we separated. Modifications will not affect the relationship table

    reply
    0
  • PHPz

    PHPz2017-04-18 10:35:38

    Depending on business needs, I personally think it’s better to be more detailed

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:35:38

    I feel that these two pieces of data are different in terms of the later needs you mentioned. The user with ID 1 must have more than one friend with ID 2
    Furthermore, when Li realizes other needs later, you write Not good together

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:35:38

    In my opinion, it is necessary to separate

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:35:38

    Two items.
    1. The other person is your friend, but you may not be the other person’s.
    2. The other party’s settings for you may not be the same as your settings for the other party.

    If you have these two needs. Two, the opposite is required, one.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:35:38

    My experience is that 2 is better. First: You are my friend, but I may not be your friend. Second: It’s so convenient to check friends this way, but it’s troublesome to check just one link.

    In addition, let’s consider expansion. If we change the friends to a blacklist, then using 2 items, it can be easily expanded. After all, if I block you, you may not block me.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:35:38

    Two, similar to following. If you follow him, he may not follow you

    reply
    0
  • Cancelreply