|
|
MySQL Database question
|
|
|
|
Senior User
Join Date: Feb 2003
Location: Pittsburgh, PA
Status:
Offline
|
|
So I am working on a MySQL DB and am trying to figure something out / I don't know what it is called or what to search for to find my answer...
It's a list of dealers, w. a field for their name, address, etc, what I need to add is a field for what brands they sell, which I will call A B and C, now it's obvious how to assign a value of a single brand, but how would I make it so that a dealer could sell both brands A and B, or A and C, etc?
And then how would I search for these values, I can probably figure it out once I can visualize how I would do this in the DB, but for now I am confused.
Thanks for any help!
|
15" MacBook Pro 2.0GHz i7 4GB RAM 6490M 120GB OWC 6G SSD 500GB HD
15" MacBook Pro 2.4GHz C2D 2GB RAM 8600M GT 200GB HD
17" C2D iMac 2.0GHz 2GB RAM x1600 500GB HD
|
|
|
|
|
|
|
|
Junior Member
Join Date: May 2003
Status:
Offline
|
|
Well, I can think of a few ways to do this. You could have a column for each brand, with a boolean indicating whether the dealer sells that brand. Pretty easy to do searching, but a pain in the butt to add new brands.
Alternatively you could have a separate table with two columns, dealer and brand, with a row for each dealer/brand pair. This would be a little more complicated and require you to do a join on the search, but is more extensible if you get to have too many brands
|
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Feb 2008
Location: Standing on the shoulders of giants
Status:
Offline
|
|
Why don't you have a brands table? You could then have a dealer's id along with a brand id. Easy to add a brand, easy to add a dealer, easy to have a dealer selling several brands.
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
What you want is a many-to-many relationship (that's the Googlable term ). As patman and mattyb said, having a junction table representing the relationship between dealers and the brands they sell is the best way to go about it.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Mar 2001
Location: yes
Status:
Offline
|
|
Yeah, what everybody has said is the way to do it, this is the essence of what relational databases are all about. To do it another way (such as serializing your data and throwing this all into a single field) would go against this design.
|
|
|
|
|
|
|
|
|
Posting Junkie
Join Date: Oct 2005
Location: Houston, TX
Status:
Offline
|
|
In the dealers table you could create a boolean column for each brand.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Originally Posted by mduell
In the dealers table you could create a boolean column for each brand.
That's not a long-term solution. You don't want to alter your schema every time you add a new brand.
Dealer table has a dealer_id primary key
Brand table has a brand_id primary key
Dealer_Brand table has two columns, dealer_id, brand_id
To add a dealer, put it in the dealer table
To add a brand, put it in the Brand table
To add a brand to a dealer, add to the mapping table
To get all brands for a dealer:
select b.*
from Brand b, Dealer_brand db
where b.brand_id = db.brand_id
and db.dealer_id = 1
That will give you all the brands for dealer with dealer_id 1. Etc.
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally Posted by mduell
In the dealers table you could create a boolean column for each brand.
Besides what Arkham said, that also doesn't make very good use of a relational database. For example, how do you get a list of all the brands a dealer has? You'd either have a massively overcomplicated SQL query or you'd be moving your relationship logic outside the database. A junction table might seem more complicated, but it's actually easier in the long run.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|