Question : Table design to enforce interesting constraint logic

I have a situation that I'm not sure I can enforce via pure table design. But I'm interested if someone can solve this for me. If not, we'll just use application logic to fully enforce it. I'm going to use two examples. The first example is simpler and I will show how I solved it. The second example is the one that I actually need to enforce.

Example 1:

Contracts are assigned to unique combinations of Equipment, Rooms and Window Type. Equipment and Rooms are always required but Window may not be.

For this, I would set up a table with a unique index on the following fields:
 - ContractID not null,
 - EquipmentID not null,
 - RoomID not null,
 - WindowTypeID null
I can't use a primary key since WindowID is nullable.

Example 2:

Same as above except that there may be multiple Window Types for a given contract. In pseudo-code, I want the following:
 - ContractID not null,
 - EquipmentID not null,
 - RoomID not null,
 - WindowTypeList null
where WindowTypeList represents a unique combination of 1 or more WindowTypes.

What I haven't figured out is how to model WindowTypeList so the table above enforces the unique combination of Equipment, Rooms and (1 or more) Window Types.

BTW, I'm doing this with SQL Server 2005.

Thanks.

Answer : Table design to enforce interesting constraint logic

Thanks Greg. I played with this some since posting and came up with that as part of the solution. What I ended up with was:

ContractAssignment - unique index on all four columns
 - ContractID not null,
 - EquipmentID not null,
 - RoomID not null,
 - WindowTypeListID null

WindowsTypeList - primary key on WindowsTypeListID
 * WindowTypeListID not null

WindowsTypeListDetail - composite primary key on WindowsTypeListID and WindowsTypeID
 * WindowTypeListID not null,
 * WindowTypeID not null

The WindowsTypeList table is needed so I can enforce the relationship between ContractAssignment and WindowsTypeListDetail. I can't join directly since WindowsTypeListDetail can have multiple records for a given WindowTypeListID.

Thanks.
Random Solutions  
 
programming4us programming4us