是否需要避免多对多关系?如果是,有什么替代方案?

发布于 2024-09-29 16:54:12 字数 218 浏览 10 评论 0原文

有人给我留下了一条评论,指出要避免许多关联,但我找不到给我留下此信息的原始评论/用户。所以我问社区,是否应该避免多对多关联,如果是的话,为什么,还有什么替代方案?

示例:

一家餐厅提供多种类型的汉堡,可以搭配多种不同的调味品。

这不是多对多关联吗?是否不需要包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

記憶穿過時間隧道 2024-10-06 16:54:13

这并不是一个明确的答案,但有时我发现我从多对多关系开始 - 例如,在购物网站上,订单到产品。然而,当我开发应用程序时,我发现链接不仅仅是一个链接,它本身就是一个实体,具有自己的属性和行为;在这种情况下,它会被称为“线”之类的东西。因此,如果我在第一种情况下正确地考虑了类似的事情,那么我就不会做出多对多的事情。

但当然,情况往往并非如此。

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.

相对绾红妆 2024-10-06 16:54:13

所以我问社区,是否应该避免很多很多的关联

我正在使用的现实世界数据库确实代表了多对多关系。

如果是这样,为什么,还有什么替代方案?

与第三个表(也称为“连接表”)的两个一对多关系,这就是在物理级别表示多对多关系的方式。

So I'm asking the community, are many-many associations to be avoided

Real world databases I'm working with do have many-to-many relations represented.

if so why, and what is the alternative?

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.

贱贱哒 2024-10-06 16:54:12

如果你想建立像你提到的汉堡/调味品这样的关系模型,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.

往日 2024-10-06 16:54:12

是的,汉堡和调味品之间的关系是多对多的。
是的,您将需要第三个表来链接这两个表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文