Multiple NULL Values ​​in a UNIQUE Column

We have a table that has a unique index in the column, which can take zero values. The problem is that we found that this structure can only accept one row with a NULL value. If we try to add a second row with a NULL value, we get this error. "cannot insert duplicate key string in object ..."

Is there anything we can do to keep the index in this column and the ability to add NULL values ​​to more than one row?

+5
source share
1 answer

Yes, you can use a filtered index to support this. Just strip the existing index and create a new index like this

CREATE UNIQUE INDEX Index_Name ON TableName(ColumnName)
WHERE ColumnName IS NOT NULL

NULL. , .

http://blog.sqlauthority.com/2008/09/01/sql-server-2008-introduction-to-filtered-index-improve-performance-with-filtered-index/

+25

All Articles