I have the following five tables:
- PRODUCT
- FEATURE
- PROVIDER
- PROVIDER_PRODUCT
- PROVIDER_FEATURE
The logic is as follows:
A generic PRODUCT
can have one or more FEATURE
s. There are different PROVIDER
s who are delivering the same product, but with different details, and the same FEATURE
s, also with different details.
The problem is that with this ERD I can create a PRODUCT
p1 with FEATURE
f1 and a PRODUCT
p2 with FEATURE
f2. Then I create PROVIDER
pv1. Now I can create a PROVIDER_PRODUCT
pvp1 with PROVIDER
pv1 and PRODUCT
p1. But when I create a PROVIDER_FEATURE
pvp1 I should only be allowed to add FEATURE
f1, as this is the FEATURE
that is linked to p1 via the PROVIDER_PRODUCT
table. However I can also add a FEATURE
f2.
How can I create a constraint that prevents the user from entering a PROVIDER_FEATURE
that is part of a FEATURE
that is part of a PRODUCT
but that is not the PRODUCT
on the PROVIDER_PRODUCT
that is on that PROVIDER_FEATURE
? Do I need to solve that in a stored procedure, or is there a more elegant way to enforce this?
+-------------------+ +--------------------+
| | | |
| PRODUCT +-----> | FEATURE |
| | | |
+---------+---------+ +----------+---------+
| |
| |
v v
+-------------------+ +--------------------+
| | | |
| PROVIDER_PRODUCT +-----> | PROVIDER_FEATURE |
| | | |
+-------------------+ +--------------------+
^
|
|
+---------+---------+
| |
| PROVIDER |
| |
+-------------------+
PROVIDER_FEATURE
? Can't you derive this information via the other tables, or are there additional attributes involved?PROVIDER_FEATURE
can be expressed as a view. Something like:CREATE VIEW PROVIDER_FEATURE AS select <product_key>, <feature_key> from PROVIDER_PRODUCT join PRODUCT on ... join FEATURE on ...
.