r/DatabaseHelp • • May 11 '15
Foreign Keys for multiple Primary Keys
I have two tables that I want to link with a third table.
The first table has 1 Primary Key (pk), no problem. I create a Foreign Key (fk) for it in the new table.
The second table has a 3 column composite pk. I am getting errors when I try to make 3 fk in the new table linking to them which may be user error (syntax or something) or impossible?
Any help would be appreciated.
1
Upvotes
4
u/muchargh May 11 '15
Quick... before the "composite keys are evil" crowd gets here.
When creating a FK reference to the composite PK, there can only be one reference - you're not enforcing those columns separately, you're enforcing the key. So generally the syntax will be something along the lines of:
CONSTRAINT FK_Noun_SomethingSomething_OtherNoun FOREIGN KEY (Column1, Column2, Column3) REFERENCES OtherTable (Column1, Column2, Column3)
1
u/r3pr0b8 May 11 '15
would help us to see your sql and the error message