A one to many relationship between two tables should be implemented with two or three tables?
For example should we have:
author(id,otherAttributtes)
books(id,authorid,otherAttributes)
or
author(id,otherAttributtes)
books(id,otherAttributes)
authorConnectsBooks(authorid,booksid)
I like more the first approach but i have seen the second and in more complicated applications a lot of times. Is there any downside for the first method, or it’s just personal which way to follow?
The first example shows a one to many relationship, while the second shows a many to many relationship.
Example lets say we use the first example
How would you represent that both Jane and Jon wrote the book “Stackoverflow for fun”? In this relationship table you cannot, you have expressed that one author can write many books. So either Jane wrote it or Jon wrote it. If only one of them wrote the books you could use this relationship type. However, if you want to show that both wrote this book you need a many to many relationship.
Now using this same analogy of Jane and Jon you can represent both authors to this one book using your second example – many to many relationship.
Lets use Stackoverflow as an example starting with a one to many relationship and ending with a many to many relationship:
Poor Jeff, he is not credited with stackoverflow from the above example…so we need to fix that:
Now everyone’s happy…