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

后端 未结 4 1749
无人共我
无人共我 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 20:45

    Since a book can have multiple authors, and an author can write more than one book, I'd suggest a many-to many relationship between the Books and Authors tables.

    Add another table which links the two, like so:

    BookAuthors

    | BookID | AuthorID |

    BookID is a foreign key to Books.id and AuthorID is a foreign key to Authors.id.

    If you want to return all of the authors for a given book, you could do something along the lines of:

    SELECT Authors.id, Authors.FirstName, Authors.LastName
    FROM Authors INNER JOIN BookAuthors ON Authors.id = BookAuthors.AuthorID
    WHERE BookAuthors.BookID = '123'
    

提交回复
热议问题