Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. DB Design

DB Design

Scheduled Pinned Locked Moved Database
databasehelpquestiondesigntutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    monrobot13
    wrote on last edited by
    #1

    I'm designing a database (at which I do not have much knowledge) and I have a question. I want to have a table that has properties, such as title, author, length, etc for books and one of the properties I want is genre (fiction, non-fiction, thriller, etc). Here's my problem, each book could be have than one category so how would I set that up? I want to have the Books table with a Genre field which would be a FK to the Genre table. What I'm not sure about is storing the values because of normalization ie. if I just stored the GenreID's as a comma seperated list in the Genre field wouldn't that break normalization not to mention make it harder to parse...I'm just a little confused with how to design this and any help would be appreciated. Thanks.

    - Aaron

    I C 2 Replies Last reply
    0
    • M monrobot13

      I'm designing a database (at which I do not have much knowledge) and I have a question. I want to have a table that has properties, such as title, author, length, etc for books and one of the properties I want is genre (fiction, non-fiction, thriller, etc). Here's my problem, each book could be have than one category so how would I set that up? I want to have the Books table with a Genre field which would be a FK to the Genre table. What I'm not sure about is storing the values because of normalization ie. if I just stored the GenreID's as a comma seperated list in the Genre field wouldn't that break normalization not to mention make it harder to parse...I'm just a little confused with how to design this and any help would be appreciated. Thanks.

      - Aaron

      I Offline
      I Offline
      i j russell
      wrote on last edited by
      #2

      Don't store the data as a a comma separted list. You need a Book_Genre table that stores the book_id and the genre_id, is covered by a unique index and links to the Book and genre tables on the key fields.

      1 Reply Last reply
      0
      • M monrobot13

        I'm designing a database (at which I do not have much knowledge) and I have a question. I want to have a table that has properties, such as title, author, length, etc for books and one of the properties I want is genre (fiction, non-fiction, thriller, etc). Here's my problem, each book could be have than one category so how would I set that up? I want to have the Books table with a Genre field which would be a FK to the Genre table. What I'm not sure about is storing the values because of normalization ie. if I just stored the GenreID's as a comma seperated list in the Genre field wouldn't that break normalization not to mention make it harder to parse...I'm just a little confused with how to design this and any help would be appreciated. Thanks.

        - Aaron

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        This is a many-to-many join that you are describing:    One book can have many genres. One genre can contain many books. What you need is an intermediate table. These are necessary to model many-to-many joins. The intermediate tables contains a compound primary key made up of the primary keys of the tables that are being joined together. In this case a Book table and a Genre table. The intermediate table BookGenre will look like this:

        BookId int not null PK
        GenreId int not null PK

        To join a book to a genre you create a new row in the BookGenre table with the corresponding primary keys. The act of making the compound primary key in the intermediate table ensures that you don't have duplicate joins. e.g. Terry Prattchet's Thump listed as Fantasy twice, or vice versa. Does this help?


        Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

        M 1 Reply Last reply
        0
        • C Colin Angus Mackay

          This is a many-to-many join that you are describing:    One book can have many genres. One genre can contain many books. What you need is an intermediate table. These are necessary to model many-to-many joins. The intermediate tables contains a compound primary key made up of the primary keys of the tables that are being joined together. In this case a Book table and a Genre table. The intermediate table BookGenre will look like this:

          BookId int not null PK
          GenreId int not null PK

          To join a book to a genre you create a new row in the BookGenre table with the corresponding primary keys. The act of making the compound primary key in the intermediate table ensures that you don't have duplicate joins. e.g. Terry Prattchet's Thump listed as Fantasy twice, or vice versa. Does this help?


          Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

          M Offline
          M Offline
          monrobot13
          wrote on last edited by
          #4

          This is exactly what I'm looking for, thanks very much for the help. It makes a lot of sense, but it's not something I would have thought of. Thanks again!

          - Aaron

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups