Results 1 to 6 of 6

Thread: Database structure and table logic - need advice

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Database structure and table logic - need advice

    Hi all,

    I need some advice about how to structure my database. It's a MySQL db and I use PHP to interact with it. The project is a business directory.

    I have a table which contains all the business info such as name,tel,email,web,text,category. I also have a category table that contains the category names, such as construction, i.t, retail etc.

    The problem is, I want to be able to have a business appear under several categories, not just one. At the mo an ID of the category is stored in the business info table, this ID refers to a category name in the category table.

    I was thinking i could store more than one value in the category field e.g. '1,4,2,5' then split them when needed. Or having more fields in the table such as cat1,cat2,cat3. What do you think?

    Thanks all
    Chris

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Database structure and table logic - need advice

    I would suggest you remove the Category from the Business table and add a third table called BusinessCategories

    At minimum the table fields would be
    BusinessName - or whatever the PK is of the Business Table
    CategoryName - or whatever the PK is of the Category Table

    Create a unique constraint on BusinessName + CategoryName

    add more fields if needed to describe a single Business -> Category relation. IE Active_Indicator, Effective_Date

    Using a third table does not put any restrictions on the number of Categories a Business can be placed.

    To select all businesses under a specific category is now simply
    Select * From BusinessCategories Where CategoryName = "xxxx"

    Rather than
    Select * From Business Where Cat1 = "xxxx" or Cat2 = "xxxx" or Cat3 = "xxxx"

  3. #3

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Database structure and table logic - need advice

    thanks for the info theres a couple things i'm not sure about...

    1. What does the unique constraint do? I looked it up and seems that is doesnt allow data to be entered that's already in the table.

    2. Select * From BusinessCategories Where CategoryName = "xxxx"
    Wouldnt this return all the business ID's (PK of businesses table) and also the category IDs? Then I would have to loop through each business ID with the Select * from Businesses Where ID = $ID to get the info about the business? Would this be too inefficient? Or have I missunderstood.

    3. Are you saying the third table should look like:
    Code:
    Business ID (PK of businesses), Cat ID (PK of Cats)
    1,2
    1,4
    1,7
    2,3
    3,7
    3,8
    4,9
    In the above example business #1 would belong to three categories.

    Cheers mate
    Chris

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Database structure and table logic - need advice

    1. You've got it right - it stops duplicate rows of data (which wont do you any good, and will add problems when editing rows).

    2. That example was only to get the PK's, to get all the data you would use something like this:
    sql Code:
    1. SELECT b.businessname, .... , c.CategoryName
    2. FROM business b
    3. LEFT JOIN BusinessCategories bc ON (b.BusinessID = bc.BusinessID)
    4. INNER JOIN categories c ON (bc.CatID = C.CatID)
    Note that the business will be shown 3 times if it belongs to 3 categories, so you would probably load the category data separately, eg:
    sql Code:
    1. SELECT c.CategoryName
    2. FROM categories c
    3. LEFT JOIN BusinessCategories bc ON (bc.CatID = C.CatID)
    4. WHERE bc.BusinessID = <the BusinessID from your Business recordset>

    3. Yes.
    Last edited by si_the_geek; Mar 1st, 2007 at 04:51 PM.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Database structure and table logic - need advice

    Quote Originally Posted by brucevde
    ...Create a unique constraint on BusinessName + CategoryName.
    Wouldn't that just be a primary key (actually a composite primary key) on those two fields.

    As it stands with the latest post those two columns are.

    Business ID (PK of businesses), Cat ID (PK of Cats)

    Wouldn't that simply be the primary key - those two columns? That gives you the unique constraint at the same time.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Database structure and table logic - need advice

    Thanks all i'll give it a try, I never knew about the JOIN keyword, what I was doing before was loading all of the categories into an array, then extracting the category from the array when printing it to the table.

    BTW... is it possible to make a composite primary key in MySQL? I use phpmyadmin to create the db.

    Also: Is there any real benefit in doing this (applying unique constraint or composite PK)? I am the only user that will ever admin the database, apart from others that will have limited access through my forms (not phpmyadmin), where I can apply validation to ensure no duplicates are made. I think it's just making things more complicated.
    Chris

Posting Permissions

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



Click Here to Expand Forum to Full Width