Database for PHP book search, how to store multiple and different numbers of authors depending on book

StackOverflow https://stackoverflow.com/questions/22145418

  •  19-10-2022
  •  | 
  •  

I am just looking advice on what would be the best way to store books with multiple authors. at the moment i have it like BookID, Title, Author, Year, (and so on) but i was asked what if a book had more than one author and i wanted to search the data base for a book with another author. It would be silly to have like BookID, Title, Author, Author1, Author2, Autho3r, Author4, Year, most of the time anything after author1 would be null.

I know what i just explained might be confusing but what would be best way to store multiple and varying numbers of authors.

Thanks

有帮助吗?

解决方案

You will need three tables for this.

- books
- authors
- author_book

The books table stores information about the book only.

The authors table stores information about the author only.

The author_book table stores links between the two tables, containing only their ids (author_id, book_id)

This is common practice, and is known as a many-to-many relationship.

See here for more info: http://en.wikipedia.org/wiki/Many-to-many_(data_model)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top