Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Foreign key constraints

Status
Not open for further replies.

pto

IS-IT--Management
Feb 14, 2002
37
US
I am very new to sql. I have a programmer who has a database with a table that has 1 primary key and 4 foreign keys. He wants to make sure that if a field is deleted that an error comes up. However, I was under the impression that a table should only have one primary key and one foreign key. Is this right? I would appreciate any feedback. [sadeyes]
 
Actually in my experience it is not uncommon to have a table that has multiple foriegn keys in it - when you normalize your database often you are representing entities that are composed of a number of pieces. Often the pieces should be represented in separate tables so as to avoid duplication. In this case the table representing the whole would have foriegn key fields for each of the pieces.

So if you are trying to represent a computer you might need to keep track of pieces like processors, motherboards, cases etc.

You could do the following
Computer(ID,processorname,processorspeed,processorpins, motherboardname,motherboardmaker,.........casecolor)

All in one table but you would be duplicating information what you probably want is something more like this

Processor(ID,name,speed,pins..........)
Motherboard(ID,name,maker,...........)
Case(ID,color........)
Computer(ID,processorID,motherboardID,caseID)

Then when you want the complete info about the computer you join the tables computer,processor,motherboard, and case and bring all the information together.


I hope this helps a little bit Crystal
crystalized_s@yahoo.com

--------------------------------------------------

Experience is one thing you can't get for nothing.

-Oscar Wilde

 
Thanks for the assistance.[2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top