Skip to content
Advertisement

How to create a constraint for conditional unique values?

I am using SQL Server. I have this table:

I want to have unique(FirstName, LastName) students only if Active = 1. If they are inactive, unique constraint should not trigger. Any idea?

Advertisement

Answer

You can’t create a CONSTRAINT for that, however, you can create a filtered unique index:

I however, highly recommend against the thought that names are unique. I (personally) shared my name and date of birth with another person at several places in my youth and businesses that treated names (and date of birth) as unique on their systems were such a head ache for the both of us (there really were places where I (or they) couldn’t register without using an abbreviated name because we “already existed”).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement