Why no many-to-many relationships?

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.


Normally (pun intended) you would use a link table to establish many-to-many

Like described by Joe Stefanelli, let's say you had Authors and Books

SELECT * from Author
SELECT * from Books

you would create a JOIN table called AuthorBooks

Then,

SELECT * 
FROM Author a 
JOIN AuthorBooks ab
  on a.AuthorId = ab.AuthorId 
JOIN Books b
  on ab.BookId = b.BookId

hope that helps.


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.

Tags:

Database

Sql