0

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?

1 Answer 1

1

Use a unique filtered index:

create unique index uc_MyTable_ItemID_For_PrimaryCategory
  on MyTable(ItemID)
where IsPrimaryCategory = 1
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. Had no idea that was possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.