是否需要避免多对多关系?如果是,有什么替代方案?
有人给我留下了一条评论,指出要避免许多关联,但我找不到给我留下此信息的原始评论/用户。所以我问社区,是否应该避免多对多关联,如果是的话,为什么,还有什么替代方案?
示例:
一家餐厅提供多种类型的汉堡,可以搭配多种不同的调味品。
这不是多对多关联吗?是否不需要包含 BURGER 表和 CONDIMENT 表的外键的关系表?
提前致谢,
BW
I was left a comment that many-many associations are to be avoided, but I cannot find the original comment/user who left me this info. So I'm asking the community, are many-many associations to be avoided, if so why, and what is the alternative?
example:
A restaurant offers many types of burgers which can be dressed with many different condiments.
Is this not a many-to-many association? Does it not require a relation table that contains the foriegn keys for the BURGER table and the CONDIMENT table?
Thanks in advance,
BW
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这并不是一个明确的答案,但有时我发现我从多对多关系开始 - 例如,在购物网站上,订单到产品。然而,当我开发应用程序时,我发现链接不仅仅是一个链接,它本身就是一个实体,具有自己的属性和行为;在这种情况下,它会被称为“线”之类的东西。因此,如果我在第一种情况下正确地考虑了类似的事情,那么我就不会做出多对多的事情。
但当然,情况往往并非如此。
This is not really a definite answer, but sometimes I find that I start off with a many-to-many relationship - for example, on a shopping website, orders to products. However, as I develop the application, I find that the link is more than just a link and is an entity in its own right that has it's own attributes and behaviours; in this case it would be called something like "line". So if I had thought through things like that properly in the first case, then I woudn't have made it many-to-many.
But of course, often that isn't the case.
我正在使用的现实世界数据库确实代表了多对多关系。
与第三个表(也称为“连接表”)的两个一对多关系,这就是在物理级别表示多对多关系的方式。
Real world databases I'm working with do have many-to-many relations represented.
Two one-to-many relations with a third table (aka the "join table"), which is anyway how you represent a many-to-many relation at the physical level.
如果你想建立像你提到的汉堡/调味品这样的关系模型,M:N 关联是不可避免的。 M:N 关联本质上并没有什么不好的地方。是的,他们需要第三张桌子。
人们常常尝试在关系上添加额外的属性,错误地将两个 1:N 关联建模为 M:N 关联。也许这就是OP关于避免M:N关联的意思。
M:N associations cannot be avoided if one wants to model relationships like the burger/condiment one you mentioned. There is nothing intrinsically undesirable about M:N associations. And yes, they need a third table.
Often people try to put extra attributes on the relationship, incorrectly modeling two 1:N associations as an M:N one. Maybe this is what the OP meant about avoiding M:N associations.
是的,汉堡和调味品之间的关系是多对多的。
是的,您将需要第三个表来链接这两个表。
Yes, the relation between the burger and condiment is many-to-many.
And yes, you will require a third table for linking these two tables.