i am not expert with indexing. I would like to create a composite key unique constraint. How to create if one of the column is nullable?
CREATE UNIQUE CLUSTERED INDEX [IX_User_Email_PeronsId] ON [dbo].[User]
(
[Email] ASC,
[PersonId] ASC
)
GO
PersonId is nullable column
In fact you can create a unique clustered index with nullable columns, just tried it:
You didn't mention what exactly you are trying to achieve, so let me have a guess. I think you want to achieve, that the combination of Email and PersonID has to be unique, except for the rows where PersonID is null.
In this case, using a clustered index is not useful, but you can use a filtered nonclustered index:
Test whether you can insert which data:
Content of the table after step-by-step execution: