Reputation: 3747
I have a table with these fields: ItemID, CategoryID, IsPrimaryCategory.
The combination of ItemID and CategoryID should always be unique and that's the easy part. But I also want a constraint to force just ItemID to be unique but only when IsPrimaryCategory is true.
How could I best accomplish this?
Upvotes: 0
Views: 26
Reputation: 89091
Use a unique filtered index:
create unique index uc_MyTable_ItemID_For_PrimaryCategory
on MyTable(ItemID)
where IsPrimaryCategory = 1
Upvotes: 1