SQL Server has a quirk with optional unique columns: you cannot have more than one null
in a table.
For example:
create table employees (
id int identity(1,1) primary key,
givenname nvarchar(24) not null,
familyname varchar(24) not null,
tfn char(9) unique -- optional, but unique
);
(In Australia, we have a Tax File Number, which is unique to the tax payer).
The following is a workaround:
create table employees (
id int identity(1,1) primary key,
givenname nvarchar(24) not null,
familyname varchar(24) not null,
tfn char(9)
);
CREATE UNIQUE NONCLUSTERED INDEX uq_employees_tfn ON employees(tfn)
WHERE tfn IS NOT NULL;
Is it possible to name a unique constraint and give it the above condition is a manner similar to the first example? That is, is it possible to include the more complex constraint inline with the column definition?