LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-16-2007, 07:16 PM   #1
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Rep: Reputation: 16
Unhappy Help with MySQL - how to design table?


Hello everyone,

I am making a database where businesses are listed and displayed on a web page (PHP-MySQL). Each business has to be in a category, like "Joe's Mechanics" is in the "Automotive services" category.

The problem I have is that I don't know the best way to design a table that supports a business being in multiple categories!

For example: the business "The Mannog Place" is in the category "Restaurant" and also in the categories "Accommodation" and "Sport and Recreation" because is has all of these facilities.

I have 30+ categories here and I need to know how best to design the table to handle them. Do I have just a 'categories' column with each category the business is in separated by commas in a long string? Or do I have 30+ colums all being a separate category with a 'yes' in them if the business is part of that category?

It seems so inefficient! Both of my ideas do.

Can anyone with more experience please help me out here?

Thanks!!

Last edited by socceroos; 02-16-2007 at 07:18 PM.
 
Old 02-16-2007, 07:30 PM   #2
excel28
Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 72

Rep: Reputation: 15
You can make 3 tables. One for the business, one for the categories, and then the last one will be the relationship.
 
Old 02-16-2007, 07:31 PM   #3
docalton
Member
 
Registered: Dec 2002
Location: St Louis, MO
Distribution: Arch Linux
Posts: 99

Rep: Reputation: 15
My first thought would be to have a table with the businesses in it. In the table give each a unique number (indexed unique).

The second table would be the various categories with each category having a unique number(indexed unique).

A third table that just have the unique business# and the categories (both fields indexed, non unique obviously). That way, any business could be in any number of categories.

This is just one of many ways this could be accomplished.

Hope this helps
 
Old 02-16-2007, 08:54 PM   #4
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
Excel28: I'm sorry but I don't understand the concept you're alluding to. What is the relationship table?

docalton: I don't understand your method either - treat me like a n00b, I have very limited understanding of MySQL and database systems in general. So if you could explain your method a bit more it would be much appreciated!

Thanks for your very quick response guys!
 
Old 02-16-2007, 09:20 PM   #5
docalton
Member
 
Registered: Dec 2002
Location: St Louis, MO
Distribution: Arch Linux
Posts: 99

Rep: Reputation: 15
What excel28 said and what i said are pretty much the same. He's just not a long winded as I. (jealousy rears its ugly head)


Business table

busid name address whatever other fields you need
------------- ---------------- ---------------- -------------------------------
1 chicken castle 1 bockbock drive
2 MS 2 bigbucks lane



category table

catid Category
------------- ----------------
1 Dining
2 Monopolistic Dictator
3 Spyware Attractor


Relation Table

busid catid
------------ ------------
1 1
2 2
2 3

The relation table essentially tells how to relate the business table and the category table
In this case chicken castle is a dining establishment, while MS is listed as both a Monopolistic Dictator and a Spyware Attractor.

Hope this helps
 
Old 02-16-2007, 09:41 PM   #6
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
Great, thankyou for explaining that so well! Very easy to understand.

Its exactly what I needed.

Thanks again for your help!
 
Old 02-16-2007, 10:02 PM   #7
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
Another related question:

With the three databases, do I need Foreign Keys and References?
 
Old 02-17-2007, 02:08 PM   #8
robbbert
Member
 
Registered: Oct 2005
Location: Hannover, Germany
Distribution: Let there be Ubuntu... :o)
Posts: 573

Rep: Reputation: 30
Quote:
With the three databases, do I need Foreign Keys and References?
Never heard of "references" in this respect, however, "foreign keys enforce referential integrity at the database level", that is, i.e., ensuring a category "in use" cannot be removed from the database. Alternatively, you can write PHP code, too, that checks necessary pre-conditions, however, it's good practice to modularize software applications -- to solve problems where they arise.

- As for the three tables, I would use two. In the business table, there would be a field for the category ID.

An Introduction to Database Normalization
 
Old 02-17-2007, 02:36 PM   #9
docalton
Member
 
Registered: Dec 2002
Location: St Louis, MO
Distribution: Arch Linux
Posts: 99

Rep: Reputation: 15
You would definately want to use foreign keys to ensure you cannot erase categories that are still in use etc.

On the database design, you could use 2 tables, 1 for business and 1 for categories. But if you are storing much data on each business with the ability of multiple categories, using 2 tables would add a lot of redundant data and end up inflating the size of your database(s).
 
Old 02-19-2007, 07:24 PM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
Just for future reference, try not to confuse using the word 'database', when you mean 'table' (& vice versa).
There are situations where this can lead to problems.
Note that a single 'physical' database eg a MySQL install will normally contain multiple 'logical' databases known as schemas, each owned by a separate user.
HTH
 
Old 02-19-2007, 10:29 PM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,352

Rep: Reputation: 129Reputation: 129
For future reference, this middle table is known as a resolver table. Its job is to resolve the many-to-many relationship that you have between business and category. If you do a search on google with resolver table mysql php tutorial then you should get a number of helpful sites that will explain this table from different angles.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Web Design Software for interfacing to MySQL on Linux with APACHE John Tulodziecki Linux - General 5 09-06-2005 12:06 AM
PHP and MySQL for web design - installing and further r3dhatter Programming 1 06-24-2004 02:10 AM
MySQL non-realtime table-by-table mirroring Passive Linux - Software 1 01-20-2004 12:11 PM
How to import MS ACCESS Table including OLE filed into the MySQL Table ? myunicom Linux - General 1 11-28-2003 11:30 AM
deleted mysql table in mysql now cant do anything nakkaya Linux - Software 0 03-18-2003 06:03 PM


All times are GMT -5. The time now is 05:02 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration