关于数据库表与数据透视表/连接表的关系
在数据库中,可以通过创建第三个表来创建表之间的多对多关系,该第三个表使用外键将两个表映射在一起。第三张表和原来的两张表有什么关系?
例如,如果表 A 和表 B 具有多对多关系,并且表 AB 是数据透视表,则 A->AB 关系是否始终是多对一关系,而 B->AB 关系是否始终是多对一关系?关系也总是多对一的关系?
In a database, one may create a many-to-many relationship between tables by creating a third table which maps the two together using foreign keys. What is the relationship between the third table and the two original tables?
For example, if table A and table B have a many-to-many relationship and table AB is the pivot table, is the A->AB relationship always a many-to-one relationship, and is the B->AB relationship also always a many-to-one relationship?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信简短的答案是“是”。 :)
简单地说,外键关系必须将 AB 列映射到 A 中的单个列或 B 中的单个列。但是由于 AB 的内容表示 A 和 B 之间的多对多关系,因此根据定义可以是AB 中 A 键或 B 键的多个实例。同时AB中出现重复记录,即。多次表示相同的 AB 关系将是错误的。
所以,是的:AB 两侧都是一对多。
I believe the short answer is "Yes". :)
Simply put, foreign key relationships must map an AB column to a single column in A or a single column in B. But since the contents of AB represent the many-to-many relationship between A and B, there can by definition be multiple instances of either the A key or the B key in AB. At the same time, a duplicate record in AB, ie. representing the same A-B relationship multiple times, would be in error.
So, yes: one-to-many on either side of AB.
挑剔的是,A->AB 和 B->AB 是一对多关系,而不是多对一:A 中的实体应该在 A 中出现一次,但可以在 AB 中出现多次(将其链接到几个 B 实例); B 中的实体应该在 B 中出现一次,但在 AB 中可能会出现多次(将其链接到多个 A 实例)。
Being picky, A->AB and B->AB are one-to-many relationships, not many-to-one: an entity in A should appear once in A, but can appear many times in AB (linking it to several B instances); and an entity in B should appear once in B, but again could appear many times in AB (linking it to several A instances).