Reputation: 5568
I posted a week or so about a database structure that I am trying to build. I didn't quite get into the complexity of it, so that's why I'm reposting. In the example, I used a CD collection as an example.
I understand that I need a lookup table for say my genres or publishers. That's not a big deal to manage.
But there are eight lookup tables in this project and some of the tables have 150 entries in them. As I understand what was suggested, I need a separate entry for CD and its permutations of data. So if I have say 20 entries on average in a lookup table, that's 20 x 20 x 20 x 20 x 20 x 20 x 20 x 20 permutations per CD. Or am I misunderstanding? How can I effectively create a master table to pull in all of the data without have a separate row for each permutation. Do I need to write some more complex SQL to manage all of this?
Help!
Upvotes: 0
Views: 184
Reputation: 70513
A brief overview of linking tables in SQL using the CD example.
Each disc can only have 1 label. We create a table of labels that looks like this:
Label Table
LabelID INT(11) NOT NULL auto_increment PRIMARY KEY,
LabelName VARCHAR(255) NOT NULL,
Then in the CD table we have the external reference to the Label
In CD Table
LabelID INT(11) NOT NULL,
When we want to see the results we do a join... like this:
select *
from CD
join Label on CD.LabelID = label.LabelID
One to many.
Lets say you have a one to many relationship then you need a joining table (I believe this is where you were getting confused.)
Fist we have the artist table for all artist available (note there can be more data in the artist table -- dob, home town... etc.
Artist Table
ArtistID INT(11) NOT NULL auto_increment PRIMARY KEY,
ArtistName VARCHAR(255) NOT NULL DEFAULT ''
Then we create a joining table. This will join one CD to many artists. A CD can have one or more rows in this table representing each link to an artist on the CD.
CD2Artist Table
CDID INT(11) NOT NULL,
ArtistID INT(11) NOT NULL
Of course we need an ID for the CD for this to work. In CD Table
CDID INT(11) NOT NULL auto_increment PRIMARY KEY
Then to select a list of artists for a particular CD you say
SELECT artist.artistname
from cd2artist
join artist on cd2artist.artistID = artist.artistID
where CDID = <put cd id here>
Upvotes: 1