Making Many Many-To-Many Relations in Relational Databases
Imagine you have a books
table. And you have people
table.
You want to create a relation between the two table, giving credit to the people.
So a traditional many-to-many does this:
- You create a table called
books_people
. - Each row of
books_people
have 2 references.book_id
andperson_id
.
[book]
\
[book_id, person_id]
/
[person]
If you only count authors this would work, perfectly.
But in making of a book, there are many more people involved: Translators, illustrators, etc.
And sometimes a person could be also a translator as well. Or even an illustrator, on the same book!
So, doing a simple traditional many-to-many does not capture their role.
And creating a table for each role (translators
, authors
, illustrators
) is not really feasible, and wasteful.
What do you do, then?
We add a role
column to the books_people
table.
[book]
\
[book_id, person_id, role]
/
[person]
And rename the table to involvements
for good measure!