If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > alias keys or small tables?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-04-09, 20:14
rbfree rbfree is offline
Registered User
 
Join Date: Feb 2009
Posts: 104
alias keys or small tables?

Please excuse the n00b question. If it's off topic or should go elsewhere, please let me know. Thanks.

I'm trying to call a parent table into one than one column in a table. My dilemma is whether to 1. use alias names for foreign keys in multiple fields in the same table or 2. build a small table corresponding to each column I want in the child table, enable me to indirectly link the parent table in question. (The parent table would become the grandparent in the 2nd approach.)

I'm facing this dilemma in several circumstances.


As an example, in a child table named transportsystems, I have a primary key (transportsysID) and a foreign key (equipmentID), which is the primary key to a table named equipment.

Each record in the transportsystems table describes a transportation system. Each transport system might use any number of pieces of equipment. For the sake of understanding this problem, I'll say from 1 to 2 pieces of equipment.

In transportsystems, I need two columns -- each one corresponding to a piece of equipment. I need to be able to call in the equipmentID twice into the same table -- so I can perform operations on the attributes. (For instance, I want to be able to add up the fuel use, in gallons per acre, so I can say how many total gallons per acre I'm using for a given tranportation system).

To me, the simplest approach would be to use 2 alias keys (say, equipment1ID and equipment2ID), since I can't use equipmentID twice in one table. However, at least one text I'm reading claims that one should rarely use this approach.

So, the other solution that occurs to me is to build a small table corresponding to each column that I want in transportsystems table, with table names equipment1 and equipment 2 and corresponding primary keys, equipment1ID and equipment2ID. Then, I can call the columns equipment1ID and equipment2ID into the transportationsystems table, thereby indirectly calling in the column equipmentID from the equipment table.

Which is the better approach? Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 02-04-09, 23:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the first approach is better -- join to the equipment table twice, with aliases

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-05-09, 00:11
rbfree rbfree is offline
Registered User
 
Join Date: Feb 2009
Posts: 104
thanks and

Thanks r937.

May I ask you to give me a brief explanation? I'm definitely not trying to be impetuous, but rather, trying to get a solid understanding of how database design works with physical structure, etc. (what works and why).

Again, thanks.
Reply With Quote
  #4 (permalink)  
Old 02-05-09, 00:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
SELECT ts.transportsysID
     , eq1.name AS equipment1_name
     , eq2.name AS equipment2_name
  FROM transportsystems AS ts
INNER 
  JOIN equipment AS eq1
    ON eq1.id = ts.equipment1ID
INNER 
  JOIN equipment AS eq2
    ON eq2.id = ts.equipment2ID
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-05-09, 05:53
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
If a transport system may use "any number of pieces of equipment", then you should not be defining multiple equipmentID columns in the transportsystems table. Instead you want an "intersection" or "many to many" table, like this one:

Code:
CREATE TABLE transportsystems_equipment
 ( transportsysID REFERENCES transportsystems
 , equipmentID REFERENCES equipment
 , PRIMARY KEY (transportsysID, equipmentID)
 );
You can then query:

Code:
SELECT ts.transportsysID
     , eq.name
  FROM transportsystems AS ts
  JOIN transportsystems_equipment AS tse 
       ON tse.transportsysID = ts.transportsysID
  JOIN equipment AS eq 
       ON eq.id = tse.equipmentID
If there are really only ever a maximum of 2 pieces of equipment per transport system (e.g. if a transport system is defined as a set of 2 pieces of equipment, like a line can be defined as a pair of points) then 2 columns is acceptable. But not if you think you'll ever need to add an Equipment3ID.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #6 (permalink)  
Old 02-05-09, 15:10
rbfree rbfree is offline
Registered User
 
Join Date: Feb 2009
Posts: 104
thanks

I think I'm seeing the problem, however, in my communication. The equipment table will have a record for each brand and model of equipment -- not each physical piece of equipment.

So, in the equipment table, maybe equipmentID number 22 will correspond to a "Gehl 6640E," which generally uses 20-gallons/acre, and has 80-horsepower, and functions on a maximum slope of 30-percent. (I'm faking these values.) This record with primary key=22 is unique. Maybe another record (primary key=33) will correspond to a "Caterpillar 320C fm forwarder," with z-gallons/acre, z-horsepower, etc. -- another unique type of equipment. Primary key 11 could represent the case of no equipment (with 0-gallons/acre, and null horsepower and null max slope, etc.

However, any given transportation system might use, say, 1 of the gehls and one of the caterpillars. Thus, in a given record (a given trans system), in field transportationsystems.equipment1, I would have FK=22, and in .equipment2, I would have FK 33.

And, another transportation might use two gehls and no cats. So, in a given record (a given trans system), in field transportationsystems.equipment1, I would have FK=22, and in .equipment2, I would have FK=22.

Maybe another trans system would use one cat and nothing else. So, in a given record (a given trans system), in field transportationsystems.equipment1, I would have FK=33, and in .equipment3, I would have FK = 11.

To know which types of equipment a transport system employs, don't I need to have fields made for those values? How else would these different and unique transport system be attributed the equipment they use? Wouldn't I need a field to enter each piece of equipment?

Also, the relationship between the equipment.equipmentID (parent) and the transportationsystem.equipmentID (child) is one-to-many from the perspective of the parent table (equipment). In otherwords, one record in equipment.equipmentID can correspond to one or many records in transportationsystem.equipmentID. (Any transportation system can have several identical pieces of equipment.) However, one record in transportationsystem.equipment ID can only correspond to one record in equipment.equipmentID. (An identifier for a piece of equipment used in a transportation system can only correspond to one piece of equipment in the equipment table -- since they're all unique.)

As you said, any given transportation system can have any combination of equipment -- (and I mean combination in the strict sense) -- ranging from one piece to many (though for the example, I'm saying 1 or 2 pieces of equipment).

I need a column/field in my transportationsystem table for each possible piece of equipment (2 for this example, up to 20 or beyond in real life).

For each unique transportation system (identified by transportsysID in the tranportation file), only one record per column is possible -- that is, only one instance of equipment can be represented in a field in one record. (No multi-value fields for reasons of referential integrity!) But, as I noted, I need to be able to call more than one piece of equipment for each transportation system (each unique record). So, I require more than one column. With one column available for each piece of equipment that might be used (sometimes, two of the same piece is part of a transport system), I can have a record that shows each piece of equipment involved. These pieces of equipment are attributes of the transportation system.

Last edited by rbfree; 02-05-09 at 15:16.
Reply With Quote
  #7 (permalink)  
Old 02-05-09, 16:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by rbfree
Please excuse the n00b question.
no prob


Quote:
Originally Posted by rbfree
Each transport system might use any number of pieces of equipment. For the sake of understanding this problem, I'll say from 1 to 2 pieces of equipment.
so now do you understand that each additional column is an additional INNER JOIN in the query? let me ask you -- how do you feel about constantly having to change your queries to add another join when you run across a new transport system that requires one more piece of equipment than you have columns for

what you actually have -- and which you explained in lovely detail -- is a many-to-many relationship

do some googling

you're almost beyond the n00b stage, but not quite

as soon as the many-to-many coin drops, you'll see how everything becomes simple

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 02-05-09, 17:36
rbfree rbfree is offline
Registered User
 
Join Date: Feb 2009
Posts: 104
many thanks again r937

I'll take your advice and looking into/ruminate upon this many-to-many issue... and also look into the INNER JOINS.

The many-to-many issue has me scratching my head!
Reply With Quote
  #9 (permalink)  
Old 02-05-09, 19:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
see also first part of post #5 where the many-to-many table that you need is described

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
Pingbacks are On
Refbacks are On