+ 6

many-to-many relationship

Can anyone fully explain many-to-many relationship in mysql?

20th Jan 2021, 9:17 AM
sasan zare
sasan zare - avatar
3 Respostas
+ 4
An example of a many to many relationship would be between authors and books. A book can have many authors. An author can write many books. The typical and probably best way to represent a many to many relationship is to create a bridge table. For this books to authors relationship, you could call this bridge table "book_author". Each record in the book_author table would reference a book and an author. More specifically, the table could be created with a statement like: create table book_author ( book_id references book(id), author_id references author(id) ); You might also want a composite unique constraint to indicate that the book_id and author_id pair should be unique. Duplicated bridge table records can be a pointless mess that bloats your database and complicates some application code. More details are at: https://en.wikipedia.org/wiki/Many-to-many_(data_model)
21st Jan 2021, 10:28 AM
Josh Greig
Josh Greig - avatar
+ 3
Josh Greig Perfect Thank you
21st Jan 2021, 6:59 PM
sasan zare
sasan zare - avatar
+ 1
Josh Greig answer is perfect. Is it possible to mark it as correct answer?
28th Feb 2021, 12:06 PM
Ernest Brayce
Ernest Brayce - avatar