问题描述
假设我有一个名为USER的实体,并且两个USER之间存在关系FRIENDSHIP
SO,因为我有一个表'USER'和一个关系表'FRIENDSHIP'
USER
id firstName姓氏
1 taher chhabrawala
2 john Dsouza
3 rahul singh
4 deepak patel
友谊
id id
1 2
1 3
1 4
4 1
4 3
在上表中,我正在存储相同的信息两次,即taher是deepak和deepak的朋友是taher的朋友
有没有办法减少这种冗余嗯,你一定可以假定所有的友谊是对称的,只能存储一次友谊,但这意味着当你想查询所有的Taher的朋友时,你必须在任一列中查找他的身份证。
或者你可以有一个单独的关系ID表,然后与用户的一对多关系表。这有一个优点,如果有一天你会想要这个关系,那么它将允许多人关系,并且允许你添加关于关系的元数据(何时开始,谁建议,无论如何)。
用户
$另一方面,在Facebook上,例如,我可以在某人的朋友,他们可以决定不要再回到我的朋友。如果你现在没有冗余的方法,那么你如何代表这种不相干的友谊呢?
Id名称
1 Taher
2 Deepak
关系
Id StartDate
1 2010-08-23
UserRelationship
RelationshipId UserId
1 1
1 2
suppose i have a entity called USER and a relationship FRIENDSHIP exist between two USERs SO for that i have a table 'USER' and a relationship table 'FRIENDSHIP'
USER id firstName LastName 1 taher chhabrawala 2 john Dsouza 3 rahul singh 4 deepak patel
Friendship id id 1 2 1 3 1 4 4 1 4 3
In the above table i am storing the same information twice i.e "taher is a friend of deepak and deepak is a friend of taher"
Is there any way to reduce this redundancy ?
解决方案Well, you could certainly just assume that all friendships are symmetric and store that friendship only once, but that would mean that when you want to query for all of Taher's friends, you have to look for his ID in either column.
Alternately you could have a separate table of relationship ID's, and then a one-to-many table of relationship to user. This has the advantage that it would allow multi-person relationships if someday you want that, and would let you add meta-data about the relationship (when it started, who suggested it, whatever).
User Id Name 1 Taher 2 Deepak Relationship Id StartDate 1 2010-08-23 UserRelationship RelationshipId UserId 1 1 1 2
On the other hand, on Facebook, for example, I can "friend" someone and they can decide not to friend me back. If you don't have the "redundant" approach that you're using now, how will you represent that not-yet-reciprocal friendship?
这篇关于如何表示对称的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!