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 > Foreign keys

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-18-10, 12:30
Juanster Juanster is offline
Registered User
 
Join Date: Jul 2010
Posts: 3
Foreign keys

Hello,

I have a database with these tables:

COMPETITIONS
IdCompetition (PK)
Name
...

TEAMS
IdTeam (PK)
Name
...

TEAMS_COMPETITIONS
IdTeam (PK)
IdCompetition (PK)
(FK1 - IdTeam to table TEAMS)
(FK2 - IdCompetition to table COMPETITIONS)

TROPHIES
IdTrophy (PK)
IdCompetition
IdTeam

The question is what would be the foreign key on the TROPHIES table?
a)
Have 2 foreign keys, one IdCompetition to the table COMPETITIONS
The other one IdTeam to the table TEAMS

b) Have only one foreign key:
IdCompetition, IdTeam to the table TEAMS_COMPETITION

Any idea which one is the correct???
Reply With Quote
  #2 (permalink)  
Old 07-18-10, 13:53
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by Juanster View Post
The question is what would be the foreign key on the TROPHIES table?
My assumption is that a team cannot win a trophy when it did not participate in the corresponding competition.
So I'd say a single FK needs to go to TEAMS_COMPETITIONS

Can you have more than one trophy per competition? If no, then the artificial PK on TROPHIES is not needed as well. But then the question comes up, what else do you want to store in the TROPHIES table that couldn't be stored the TEAMS_COMPETITIONS table (and maybe the competitions table)
Reply With Quote
  #3 (permalink)  
Old 07-18-10, 13:56
Juanster Juanster is offline
Registered User
 
Join Date: Jul 2010
Posts: 3
A Team can't win a trophy unless is playing in a competition, you are right.

I create a Trophies table because one team can win several trophies in the same competition and also there could be several trophies per competiton.

Thanks
Reply With Quote
  #4 (permalink)  
Old 07-18-10, 14:08
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by Juanster View Post
I create a Trophies table because one team can win several trophies in the same competition and also there could be several trophies per competiton.

Thanks
Ok, then the separate PK for the trophies table does make sense.
Reply With Quote
  #5 (permalink)  
Old 07-18-10, 14:09
Juanster Juanster is offline
Registered User
 
Join Date: Jul 2010
Posts: 3
You mean the one I have just one FK?
Reply With Quote
  #6 (permalink)  
Old 07-18-10, 15:25
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by Juanster View Post
You mean the one I have just one FK?
Yes. Keep IdTrophy and make (IdCompetition, IdTeam) a FK to TEAMS_COMPETITIONS
Reply With Quote
  #7 (permalink)  
Old 07-19-10, 10:53
Ram2Curious Ram2Curious is offline
Registered User
 
Join Date: Jul 2010
Posts: 8
Foreign key(FK) is completely based on the data you want to "Retrieve"......

When it comes to the case of "Trophies" then obviously you would want to know which team has won the Trophy....

So "Teams" idTeam(PK) would be the foreign key in "TROPHIES" table idTeam(FK)....
Reply With Quote
  #8 (permalink)  
Old 07-19-10, 14:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Ram2Curious View Post
Foreign key(FK) is completely based on the data you want to "Retrieve"......
i think i know what you wanted to convey with this statement, but it did not succeed

more correct would be to say that the FK is completely based on the data you want to integrificate

in other words, FKs are about Referential integrity

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 07-19-10, 15:37
Ram2Curious Ram2Curious is offline
Registered User
 
Join Date: Jul 2010
Posts: 8
Integrificate ???????
Reply With Quote
  #10 (permalink)  
Old 07-19-10, 15:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Ram2Curious View Post
Integrificate ???????
yes, integrificate (verb, transitive): to cause [a schema] to have relational integrity
__________________
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