java - 好友关系表数据的存储问题,存一条还是两条?
阿神
阿神 2017-04-18 10:33:38
0
7
662

项目中设计好友关系,存储在一张表里面
现有用户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

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

阿神
阿神

闭关修行中......

reply all(7)
迷茫

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

PHPzhong

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

左手右手慢动作

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

Peter_Zhu

In my opinion, it is necessary to separate

Peter_Zhu

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.

伊谢尔伦

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.

伊谢尔伦

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template