Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no many-to-many relationships?

Tags:

sql

database

I am learning about databases and SQL for the first time. In the text I'm reading (Oracle 11g: SQL by Joan Casteel), it says that "many-to-many relationships can't exist in a relational database." I understand that we are to avoid them, and I understand how to create a bridging entity to eliminate them, but I am trying to fully understand the statement "can't exist."

Is it actually physically impossible to have a many-to-many relationship represented?

Or is it just very inefficient since it leads to a lot of data duplication?

It seems to me to be the latter case, and the bridging entity minimizes the duplicated data. But maybe I'm missing something? I haven't found a concrete reason (or better yet an example) that explains why to avoid the many-to-many relationship, either in the text or anywhere else I've searched. I've been searching all day and only finding the same information repeated: "don't do it, and use a bridging entity instead." But I like to ask why. :-)

Thanks!

like image 587
The111 Avatar asked Sep 07 '11 19:09

The111


People also ask

Is many-to-many relationship possible?

A many-to-many relationship occurs when multiple records in a table are associated with multiple records in another table. For example, a many-to-many relationship exists between customers and products: customers can purchase various products, and products can be purchased by many customers.

Why is an m'n relationships not appropriate in a relational model?

M:N relationships are not appropriate in a relational model. In Chen notation, entities and relationships have to be oriented horizontally - not vertically. Today, most relational database products can be classified as object/relational. The network model has structural level dependence.

How do you deal with many-to-many relationships?

When you have a many-to-many relationship between dimension-type tables, we provide the following guidance: Add each many-to-many related entity as a model table, ensuring it has a unique identifier (ID) column. Add a bridging table to store associated entities. Create one-to-many relationships between the three tables.

How many tables are needed to form a many-to-many relationship?

Note: Minimum of three tables are required in the Many to Many relationships.


2 Answers

Think about a simple relationship like the one between Authors and Books. An author can write many books. A book could have many authors. Now, without a bridge table to resolve the many-to-many relationship, what would the alternative be? You'd have to add multiple Author_ID columns to the Books table, one for each author. But how many do you add? 2? 3? 10? However many you choose, you'll probably end up with a lot of sparse rows where many of the Author_ID values are NULL and there's a good chance that you'll run across a case where you need "just one more." So then you're either constantly modifying the schema to try to accommodate or you're imposing some artificial restriction ("no book can have more than 3 authors") to force things to fit.

like image 174
Joe Stefanelli Avatar answered Sep 20 '22 18:09

Joe Stefanelli


A true many-to-many relationship involving two tables is impossible to create in a relational database. I believe that is what they refer to when they say that it can't exist. In order to implement a many to many you need an intermediary table with basically 3 fields, an ID, an id attached to the first table and an id atached to the second table.

The reason for not wanting many-to-many relationships, is like you said they are incredibly inefficient and managing all the records tied to each side of the relationship can be tough, for instance if you delete a record on one side what happens to the records in the relational table and the table on the other side? Cascading deletes is a slippery slope, at least in my opinion.

like image 40
Mike_OBrien Avatar answered Sep 21 '22 18:09

Mike_OBrien