SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Do I need a table for this?

    The author of a book I'm reading is creating a bookstore. He creates a table for the books that references a category ID which is linked to another table with the category name. My question is, why do this? Couldn't you just store the category name in the table as long as each name is unique?

  2. #2
    <? james('rules'); ?>
    Join Date
    Jun 2004
    Location
    Wales, UK
    Posts
    789
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Show us this table..so we can understand what you mean more.
    If knowledge is power - Why isn't our army librarians?!
    Statistics show that 63% of all statistics are fake.
    When i was little i broke my neck, and i havent looked back since .
    I completed the internet in 1 week. The end boss was pretty easy though .

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,018
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by subnet_rx
    Couldn't you just store the category name in the table as long as each name is unique?
    you could, yes, but if you make the category name unique in the book table, it means you can only ever have at most one book in each category

    :-)
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    you could, yes, but if you make the category name unique in the book table, it means you can only ever have at most one book in each category

    :-)
    ohh, sorry, I didn't mean unique. I just meant that if I give each category a unique name (fiction, non-fiction, etc.) then I could just do a select based on category without having to reference a category ID and without having to create another table. But, as I type this, I think I understand why he did this, it's so he can do a query and list all categories on a web page. If he had 10 books listed under a fiction category, a query that returns just the categories is going to list fiction 10 times.

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,018
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by subnet_rx
    If he had 10 books listed under a fiction category, a query that returns just the categories is going to list fiction 10 times.
    not if DISTINCT is used
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •