How can I model a compatibility matrix database in sql server 2008;
i.e. I have 4 groups of design. If I select group 1 and group 4, my choices will influence group 2 and 3 and will make them unselectable because they are not compatible with my choices.
I’m a beginner, so my explanation might seems watery, pls forgive!
Any kind of “matrix” is essentially an M:N relationship, only in this case both “axis” of the matrix represent the same thing (“groups of design”). The M:N relationship of a table with itself can be modeled like this:
Assuming relationships are bidirectional, you should also add:
CHECK(GROUP_ID1 < GROUP_ID2)in the COMPATIBILITY table. This would, for example, allow (1, 4), but prevent (4, 1) from being in the table.The example from your question would be represented by the following data in the database:
GROUP:
COMPATIBILITY:
When user selects a group X, you’d run the following query to find which groups it is compatible with. The remaining groups are in-compatible.
For 1, this would return 4 and for 4 it would return 1. In either of these cases, 2 and 3 would not be returned – a sign they are incompatible.
On the other hand, if you want do disable 2 and 3 when 1 and 4 are selected, but not when just 1 or just 4 is selected, this is a different problem that would be more complicated to model in the relational paradigm. Let me know if that’s what you actually need…