I am grappling with a database design issue at the moment, I will present a simplified scenario of what is going on:
Lets say I have four tables, Equipment, CurrentState, StateValue, StateType with the following schemas:
Equipment
------------
Id (PK),
Name
CurrentState
------------
Id (PK),
EquipmentId (FK) (IX),
StateValueId (FK),
StateTypeId (FK) (IX)
StateValue
------------
Id (PK),
StateTypeId (FK),
Name
StateType
-----------
Id (PK),
Name
A piece of equipment can have several different CurrentStates belonging to different StateTypes, hence the Unique Index (IX). A StateType is basically a state machine and StateValue contains the values for each state machine.
Now, my question, well 2 questions, is around the StateTypeId foreign keys in the CurrentState and StateValue tables that determine what StateType a CurrentState record is and what StateType a StateValue record is.
First of all, is having this type of relationship bad design in terms of referential integrity? My guess is that it IS bad design as there should only be one link between the CurrentState table and the StateType table, and this is via the StateValue, else a CurrentState record could end up having two different StateTypes (one via the direct FK and the other via the StateValue tables FK)...
But the second question comes: If I should not have a StateTypeId FK in the CurrentState table, how can I enforce the Index, that is ensure that there is not two CurrentState records for a single EquipmentId that have StateValueIds pointing to StateValue records of the same StateType...
Do I have to use a trigger when inserting into the CurrentState table to check that the rules are followed? I have never used triggers before, so I will need to do a bit of research. I am also using Entity Framework and have no idea what the implications are with this (there shouldn't be any in theory).