Travus Gonzalez
Travus Gonzalez

Reputation: 89

How can I relate multiple ids to a single row in MS SQL Server?

I am working on a personal library database in MS SQL Server 2014 and I have two tables called Book, and Genre. The Genre table has a list of genres that a book can have. How can I let a book have multiple Genres with out repeating my attributes because each book might have a different number of Genres?

<Genre> 
ID | Name

<Book>
ID | Title | AuthorID | GenreID | GenreID2 | GenreID3 | ... etc 

Upvotes: 1

Views: 102

Answers (1)

John
John

Reputation: 384

What you need is a Cross Reference table. The table might look like this:

  <BookGenre>
  GenreId | BookId

For each genre that applies to a book, you insert a record into this table with the appropriate GenreId and BookId.

Upvotes: 1

Related Questions