How write down to database multiple authors in simple books table?

后端 未结 4 1589
无人共我
无人共我 2021-02-04 20:24

I am wondering how should I save authors (in case where there are more than one author) in simple database.

In case one one book has one authors, everything is easy. The

4条回答
  •  抹茶落季
    2021-02-04 21:00

    You have two real possibilities:

    • Many authors per book, but each author can have at most one book (highly unlikely) - this way, you'll put a foreign key in the Author table, linking that author to the book. You could have two such authors with the same book ID, but it means you don't have to duplicate the book details or, actually, anything more than the book's ID
    • More likely: Many authors per book, and many books per author. This is called a "many to many relationship", and requires that you add another table.

    So, let's think about that for a second: a book has all its details, and an ID. Each author has many details, and an ID. If you want to associate an author to a book, you add a row to another table, let's say called "Credits".

    • Each author can have many credits, but each credit is for a single author.
    • Similarly, each book can have many credits on the front cover, but each credit is only on that book.

    That way you have two one-to-many relationships (from author to credits, and from book to credits), and can represent what you need without any awkward multi-values.

提交回复
热议问题