Question : Unique index dependent on a column

Folks,

Suppose I have the following table in SQL Server:
Name     IsRetired
------      -----------
John       0
Joe         0
John       0
John       1

I want to make Name unique IF IsRetired is 1.  So the example above is fine because John appears only once when IsRetired is 1.  However, I couldn't add another John and set IsRetired to 1.

is there a way to create a unique index for this?

Answer : Unique index dependent on a column

I do not believe unique indexes can be partially unique. In your example, you have two John's with a IsRetired = 0.

You could handle this through a Insert/Update trigger, however that starts to embed business logic into the trigger, where it probably should not be.
Random Solutions  
 
programming4us programming4us