Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
If you refer a composite primary key of a master table, you have to refer all the fields of the composite primary key.
But how to partially refer the composite primary key in a child table? For example, if Table1 has primary key (A,B,C),
how to create a foreign key in Table2 which will refer only (A,B) fields of Table1?

thanks in advance,
Posted
Updated 26-Mar-13 8:28am
v2

1 solution

You can do this, but:
1) You can add reference to a property set that is unique.
2) As primary key, (A,B,C) is unique.
3) If you want to add reference to (A,B), it has to be unique
4) If (A,B) is unique thus a primary key candidate, why is (A,B,C) the primary key after all?

I smell a design problem. Please consult the following article too: http://www.sqlmag.com/article/systems-administrator/sql-by-design-how-to-choose-a-primary-key-5113[^]
 
Share this answer
 
Comments
srastogi85 26-Mar-13 14:41pm    
Thanks Zoltán
, i already had table structure having (A,B) as primary and referecned by other tables, requirement is i added 1 new table, for this new table i needed one more Value so introduced column C, primary key made as (a,b,c) ,and wanted the other relations to work existing ones with (A,B) to work as earlier.
Please suggest .
Zoltán Zörgő 26-Mar-13 14:45pm    
Why do you need this one more value? But it is your choice.
If (A,B) is still key candidate, do following:
1) let (A,B) be the primary key
2) create a unique constraint over (A,B,C)
3) use the (A,B) as referenced property set in the one, and (A,B,C) in the other table
srastogi85 26-Mar-13 14:51pm    
Thanks again , but the table where i am now going to make (A,B,C) as primary key i will be
saving multiple rows, which will have a,b,c as unique
Exact tables mentioned below:-
Table 1 -> Column A as foreign key
Table 2 -> column A,B vas primary key
Table 3 ->column A,B as foreign key
Table 4 ->column A as foreign key
Zoltán Zörgő 26-Mar-13 15:10pm    
Than use (A,B,C) as primary key, and add create a unique constraint over (A,B). But I am pretty sure you have a design problem over there.
Maciej Los 26-Mar-13 15:06pm    
Nice, +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900