Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

one to many vs many to many relationship

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.

relationship

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

onetomany

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. enter image description here

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.

like image 933
user2947249 Avatar asked Jan 28 '14 13:01

user2947249


3 Answers

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.

like image 74
Thorsten Dittmar Avatar answered Oct 20 '22 10:10

Thorsten Dittmar


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.

like image 29
Marco Bonzanini Avatar answered Oct 20 '22 08:10

Marco Bonzanini


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.

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.

like image 41
Arun Raaj Avatar answered Oct 20 '22 09:10

Arun Raaj