Difference between making a composite foreign key for all columns or a foreign key for each column

foreign key

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)

Best Answer

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.