0

In general, is there a difference between making a composite foreign key for all columns or a foreign key for each column?

First approach

FOREIGN KEY(OrderId, CompanyId) REFERENCES ParentTable(OrderId, CompanyId)

Second approach

FOREIGN KEY(OrderId) REFERENCES ParentTable(OrderId)
FOREIGN KEY(CompanyId) REFERENCES ParentTable(CompanyId)

1 Answer 1

3

Sure, there is a big difference. In first case you are referencing by combination of columns. It means ParentTable should contain a row for each combination of OrderId and CompanyId which you want to add to your ChildTable. In the second case it would be enough to have separate rows with corresponding OrderId and corresponding CompanyId.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.