SQL表中的两个条目之间的关联 [英] Association between two entries in SQL table
问题描述
我想在这种情况下,需要第二个表来存储人际关系。此表将包含两个字段(person1,person2),每个条目对应于两个人之间的一对一关系。
这是正确的还是有更多聪明的方式做到吗?这个方法使得关联表缩放为n_users ^ 2。
多对多关系。这是所有人都可以有很多朋友。
如果你有一对多的关系,像所有人有一个老板(或没有老板),你不需要额外的表,那么在person表中只需要一个BossId列。
Imagine you have a database table that stores a list of people. You want to establish a relationship between peoples, i.e., person I is friend with person J.
I suppose in this case, one needs a second table to store people associations. This table would contain two fields (person1, person2) and each entry corresponds to a one-to-one relationship between two people.
Is that right or is there a more clever way to do it? This method makes the association table scale like n_users^2.
Yes, this is right if you want to model a many-to-many relation. That is all persons can have many friends.
If you have a one-to-many relation, like all persons has one boss (or no boss) you don't need the extra table, then you only need a BossId column in the person table.
这篇关于SQL表中的两个条目之间的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!