I am not sure what will be the best way to enforce participation in the following database design (can be for SQL server, Oracle, mySQL etc.). I have two tables: Factory and Worker. I have created two rules for the design: 1) Each factory hires at least one (or many) workers. 2) Each worker works for exactly one and only one factory.
I have created an ERD for the design:
[Factory]-||--- hire ---|<-[Worker]
The FACTORY table looks like this:
1 A
2 B
3 C
The WORKER table looks like this:
1 Tom 2
2 Ann 1
3 Jan 1
In the worker table, the Factory_ID is the foreign key (FK) and it is set to NOT NULL because (the ERD said) each worker have to work for one factory and only one. This participation can be enforced by setting the FK (the Factory_ID field in the Worker table) to not null.
Here is my question, what is the best way to enforce the participation of - Each factory hires at least one workers? I.e. I need to make sure each factory_id have to be referenced in the worker table at least once. So now factory #3 does not hire anyone which violates the participation rule in the ERD.
I am wondering what will be the best way to enforce this participation?
It specifies that each entity in the entity set must compulsorily participate in at least one relationship instance in that relationship set. That is why, it is also called as mandatory participation. Total participation is represented using a double line between the entity set and relationship set.
A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables to control the data that can be stored in the foreign key table.
A check constraint is a rule that specifies the values that are allowed in one or more columns of every row of a base table. For example, you can define a check constraint to ensure that all values in a column that contains ages are positive numbers.
Yes, you can put multiple constraints on a single column.
1+ constraints on relationships are usually implemented at the application layer.
The problem is that there's a timing concept to anticipate: with this kind of restrictions you would need a worker for your factory but your worker needs a factory himself: this is just simply impossible to implement this rule a synchronous maner.
A solution could be to manage a sort of backoffice to add the elements separately (for administrative purpose) and then implement within the public front the appropriate restrictions to match the 1+ requirements you have. For example:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With