How do you deal with m..n relationships in a relational database?

前端 未结 10 1096
醉梦人生
醉梦人生 2020-12-14 18:48

Let\'s look at an example - books. A book can have 1..n authors. An author can have 1..m books. What is a good way to represent all of the authors of a book?

I came

10条回答
  •  青春惊慌失措
    2020-12-14 19:45

    What you're asking really is not how you deal with 1..n relationships, but n..n relationships (as effectively on author and have many books, and one book can have many authors).

    The classic way to handle this is via an intermediate table, so

    Author table (authorID, authorDetails) Book table (bookID, book details) AuthorBook table (authorID, bookID)

    If you're really bothered about changing author names then use a 1..n author details table, so add

    AuthorDetails (authorID, itemID, authorDetails)

    and remove authorDetails from the author table

提交回复
热议问题