Just beginning to learn about SQL and had a question I couldn’t figure out.
I have a setup based on the following tables and their primary keys, the columns with the same name between tables are constrained by foreign keys:
Company:
- CompanyId
Division:
- CompanyId
- DivisionId
Resource:
- CompanyId
- ResourceId
DivisionResource :
- CompanyId
- DivisionId
- ResourceId
- DivisionResource is used to create a many to many relation between division and resource and constrain them so that divisions can only be linked to resources of the same company.
- Without the DivisionResource, Division and Resource wouldn’t need the CompanyId as a primary key to contain unique records.
So my question is this: Is there a way to create a similar constraint as DivisionResource creates without forcing Division and Resource to have an extra column in its primary key?
ResourceCompany and DivisionCompany in the schema below are connecting tables. They will have CompanyId in their primary key but Resource and Division will have primary keys with one column. This is what you looked for.