SQL表中的两个条目之间的关联 [英] Association between two entries in SQL table

查看:176
本文介绍了SQL表中的两个条目之间的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个存储人员列表的数据库表。你想建立人与人之间的关系,即我是朋友与人J的人。



我想在这种情况下,需要第二个表来存储人际关系。此表将包含两个字段(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆