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.
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)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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With