Search code examples
sql-serverindexingnullable

Nulls in SQL Server unique index?


I have done a wholesale transfer of tables from Microsoft Access to SQL Server, and I am attempting to apply a unique index to a table field that, while it has some null values, has zero duplications among the values that are populated. Access handles this all the time without complaint, but SQL Server is setting up a conundrum, where the index is defined as allowing nulls, but returns an error message, saying that it can't build the index because it has a duplicate value of "Null". I have included an image of the creation of the index and its accompanying error message. Please help me resolve this issue. Thank you.

Error when creating image that includes nulls


Solution

  • Yeah, that is an issue where SQL Server is not following standard SQL practices. There is a solution, though. Use separate indices. You can FILTER indices.

    • A UNIQE index where the field is NOT NULL
    • A non unique index where the field IS NULL

    Done.