QMan5
QMan5

Reputation: 779

Have a relationship between one table with one primary key and another with two primary keys?

I'm asking about this generally, but I'll give an example for illustration purposes.

Table1 has the following columns:

ID (Pk)
Order_Desc
Order_DT 

Table2 has the following columns:

ID (PK)
Product_Code (PK)
Product_Desc 

Is it possible for me to have relationship between Table1 and Table2. If so, how would you do this in SQL without you running into an error? Would you be able to create a relationship if the Product Code or ID was not a primary key? Instead, it was a foreign key?

Upvotes: 0

Views: 668

Answers (1)

marc_s
marc_s

Reputation: 754268

Your table2 does not have two primary keys - it has ONE primary key made up from 2 columns. Any relational table NEVER has more than one primary key - it's just not possible at all.

Any FK relationship to that table must include all the columns that the PK of the referenced table has - so any FK to Table2 must include both ID and Product_Code.

It's an all or nothing proposition - either your foreign key includes all columns of the referenced tables primary key - or you cannot establish a FK-relationship.

Upvotes: 1

Related Questions