July 14, 2008 at 12:26 am
I have a table with a composite PK , and there is another table must has a reference to this table . the question is how to reference a composite PK,
is there is anything called composite FK if so how to create it in SQL Server 2005..
i need a solution other than inserting an auto number field to reference.
Thanks.
July 14, 2008 at 6:25 am
If a PK looks like this:
Col1
,Col2
,Col3
Then in order to reference that as a foreign key, the code would look something like this:
ALTER TABLE [schema].[tablename]
CONSTRAINT [MyFKName] FOREIGN KEY
(Col1
,Col2
,Col3)
REFERENCES [schema].[parenttable]
(Col1
,Col2
,Col3)
The names of the columns can be different, but the data types should be the same. Other than that, have fun.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
July 14, 2008 at 9:16 pm
Hi Grant,
Can a foreign key be a composite key even if the primary key of the table it references is not a composite key. For eg. If Part# is a primary key in a table. Can the foreign key be Part# + PartName + PartSerial.
Thanks in advance.
July 15, 2008 at 5:58 am
In order to use a foreign key constraint you have to have either a primary key or a unique index/constraint on the table to which you are referencing. Otherwise, you'd have to use a trigger to enforce integrity, with all the headaches and maintenance issues that it brings.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
July 15, 2008 at 6:38 am
I've seen this in Oracle, where call records (telcom application) are keyed on phone number, customer number and effective date (and possibly rate plan)
July 16, 2008 at 2:16 am
mailsar (7/14/2008)
Can a foreign key be a composite key even if the primary key of the table it references is not a composite key. For eg. If Part# is a primary key in a table. Can the foreign key be Part# + PartName + PartSerial.
No. If the primary key (or unique constraint) consists of a single column, the foreign key can only be a single column if the same datatype. If the pk/constraint consists of multiple columns, the fk can only be the same number of columns of the same datatype.
In your example, if pk is Part# but you want to refer to Part#, PartName, PartSerial from another table, define the three columns in a unique constraint or index. You can have multiple unique constraints defined for a table (the primary key is, of course, always one of them) and you can have a fk refer to any of them.
Tomm Carr
--
Version Normal Form -- http://groups.google.com/group/vrdbms
July 16, 2008 at 8:36 am
Thanks Tomm and Grant :). That makes sense. I appreciate your replies.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply