一对多与多对多关系

时间:2022-06-03 09:58:35

I just started learning database and I'm confused with using one to many vs many to many relationship.

我刚开始学习数据库,我很困惑使用一对多与多对多的关系。

so I came up with a simple example on the relation beside customer and food. Customers orders food.

所以我想出了一个关于顾客和食物之间关系的简单例子。客户订购食物。

一对多与多对多关系

1)If i use one to many relationship, I will say that A customer can order MANY food.

1)如果我使用一对多关系,我会说客户可以订购许多食物。

一对多与多对多关系

Base on the above diagram,

基于上图,

1)bob orders noodles and rice
2)jane orders noodles and crab

1)If i use many to many relationship, I will say that MANY customer can order MANY food. 一对多与多对多关系

1)如果我使用多对多关系,我会说许多客户可以订购许多食物。

1)bob orders noodles and rice
2)jane orders noodles and crab

I keep seeing it as the same thing.

我一直认为它是一样的。

bob still orders noodles and rice 
and 
jane still orders noodles and crab 

regardless whether I use one to many or many to many relationships. correct me if I am wrong.

无论我是使用一对多还是多对多关系。如果我错了,请纠正我。

3 个解决方案

#1


10  

The food sample does work well for a one-to-many relationship: One customer can order many dishes.

食物样本确实适用于一对多的关系:一个顾客可以订购许多菜肴。

The many-to-many relationship is better described by a book sample:

书籍样本更好地描述了多对多关系:

An author can write many books (that would be a one-to-many relationship). But he can have co-authors also involved - one book can have many authors.

作者可以写很多书(这将是一对多的关系)。但他也可以让合着者参与其中 - 一本书可以有很多作者。

#2


4  

The one-to-many relationship diagram is probably misleading because noodles appears twice, while you should only have one record (like you correctly have in the many-to-many diagram).

一对多的关系图可能会产生误导,因为面条出现两次,而你应该只有一条记录(就像你在多对多图中所正确的那样)。

Your example is a many-to-many relationship: a customer can order many food items, and the same food items can be ordered by many customers.

您的例子是多对多关系:客户可以订购许多食品,许多客户可以订购相同的食品。

If you model it as a one-to-many relationship, you are either saying that the customer can order only one item, or that the item can be ordered by one customer only.

如果您将其建模为一对多关系,则要么说客户只能订购一件商品,或者该商品只能由一位客户订购。

Don't confuse participation with cardinality: the term "one-to-many" says that an entity occurrence (a single record, e.g. noodles) on the "one" side of the relationship can occur only once, while an entity occurrence (e.g. Bob) on the "many" side can occur ... many times.

不要将参与与基数混淆:术语“一对多”表示关系“一”侧的实体事件(单个记录,例如面条)只能出现一次,而实体出现(例如鲍勃)在“很多”方面可能会发生很多次。

An example of one-to-many relationship, in the noodle-restaurant-chain scenario: an employee works for a branch (and one branch only), while the same branch will have many employees working there.

面条 - 餐厅 - 连锁场景中一对多关系的一个例子:一个员工在一个分支机构工作(只有一个分支机构),而同一个分支机构将有许多员工在那里工作。

#3


0  

Your Customer and Food example is many-to-many.
One Food type will be eaten by many Customer.
Similarly one Customer can eat multiple Food.

您的客户和食品示例是多对多的。许多顾客都会吃掉一种食物类型。同样,一个顾客可以吃多种食物。

so, many to many means 1-n from both the sides.

所以,很多对很多都意味着来自双方的1-n。

Author and Book is the right example of one-to-many given that Book has no co-authors. Otherwise, it is also an example of many-to-many.

鉴于Book没有合着者,作者和书籍是一​​对多的正确例子。否则,它也是多对多的一个例子。

#1


10  

The food sample does work well for a one-to-many relationship: One customer can order many dishes.

食物样本确实适用于一对多的关系:一个顾客可以订购许多菜肴。

The many-to-many relationship is better described by a book sample:

书籍样本更好地描述了多对多关系:

An author can write many books (that would be a one-to-many relationship). But he can have co-authors also involved - one book can have many authors.

作者可以写很多书(这将是一对多的关系)。但他也可以让合着者参与其中 - 一本书可以有很多作者。

#2


4  

The one-to-many relationship diagram is probably misleading because noodles appears twice, while you should only have one record (like you correctly have in the many-to-many diagram).

一对多的关系图可能会产生误导,因为面条出现两次,而你应该只有一条记录(就像你在多对多图中所正确的那样)。

Your example is a many-to-many relationship: a customer can order many food items, and the same food items can be ordered by many customers.

您的例子是多对多关系:客户可以订购许多食品,许多客户可以订购相同的食品。

If you model it as a one-to-many relationship, you are either saying that the customer can order only one item, or that the item can be ordered by one customer only.

如果您将其建模为一对多关系,则要么说客户只能订购一件商品,或者该商品只能由一位客户订购。

Don't confuse participation with cardinality: the term "one-to-many" says that an entity occurrence (a single record, e.g. noodles) on the "one" side of the relationship can occur only once, while an entity occurrence (e.g. Bob) on the "many" side can occur ... many times.

不要将参与与基数混淆:术语“一对多”表示关系“一”侧的实体事件(单个记录,例如面条)只能出现一次,而实体出现(例如鲍勃)在“很多”方面可能会发生很多次。

An example of one-to-many relationship, in the noodle-restaurant-chain scenario: an employee works for a branch (and one branch only), while the same branch will have many employees working there.

面条 - 餐厅 - 连锁场景中一对多关系的一个例子:一个员工在一个分支机构工作(只有一个分支机构),而同一个分支机构将有许多员工在那里工作。

#3


0  

Your Customer and Food example is many-to-many.
One Food type will be eaten by many Customer.
Similarly one Customer can eat multiple Food.

您的客户和食品示例是多对多的。许多顾客都会吃掉一种食物类型。同样,一个顾客可以吃多种食物。

so, many to many means 1-n from both the sides.

所以,很多对很多都意味着来自双方的1-n。

Author and Book is the right example of one-to-many given that Book has no co-authors. Otherwise, it is also an example of many-to-many.

鉴于Book没有合着者,作者和书籍是一​​对多的正确例子。否则,它也是多对多的一个例子。