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 as Primary Key

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-01-07, 13:50
techno_brains techno_brains is offline
Registered User
 
Join Date: Mar 2007
Posts: 2
Exclamation Foreign Keys as Primary Key

Hello,
Can I use 2 foreign keys from different tables as a primary key for a third table...?

Leave(leave_type, days,...)

Staff(ID, name,....)

LeaveBalance(LeaveID, balanceDays....)


I need LeaveID to be the primary key of LeaveBalance, where LeaveID comprises of leave_type from Leave and ID from Staff....

Thanks...
Reply With Quote
  #2 (permalink)  
Old 03-01-07, 14:04
RedNeckGeek RedNeckGeek is offline
Village Idiot
 
Join Date: Jul 2003
Location: Michigan
Posts: 1,941
You can have a multi-column primary key, if that's what you are asking.

LeaveBalance(Staff_Id, Leave_Type, balanceDays....)
__________________
Inspiration Through Fermentation
Reply With Quote
  #3 (permalink)  
Old 03-01-07, 14:05
techno_brains techno_brains is offline
Registered User
 
Join Date: Mar 2007
Posts: 2
Can't we have composite attributes as primary keys...?
Reply With Quote
  #4 (permalink)  
Old 03-01-07, 14:26
RedNeckGeek RedNeckGeek is offline
Village Idiot
 
Join Date: Jul 2003
Location: Michigan
Posts: 1,941
I think that may be specific to the platform you're using. I know you can't in Access or Sql Server.

Of course, every time I use "I know you can't..." somebody else jumps in with
"Sure you can..."
__________________
Inspiration Through Fermentation

Last edited by RedNeckGeek; 03-01-07 at 15:42.
Reply With Quote
  #5 (permalink)  
Old 03-01-07, 15:34
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by techno_brains
Can I use 2 foreign keys from different tables as a primary key for a third table...?
of course, yes you can
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 03-01-07, 16:07
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
What's the difference between having "composite attributes as primary keys" and a standard multi-column (composite) key?
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #7 (permalink)  
Old 03-04-07, 15:58
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 697
Hold on a second here... If I'm reading this right, "LeaveID comprises of leave_type from Leave and ID from Staff" does that mean that, say, type is a letter and ID is a number so leaveID is going to be X123 or Y456?

You can certainly make that a primary key, but most DBMS's won't let you set up a foreign key relationship that way.

What you *could* do is set up a table LeaveBalanceTbl with two columns, type and ID as the primary key. Then set up a view LeaveBalance that concatenates those columns into one. That would probably work.
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