8

We have a table that has a unique index on a column that can accept null values. Problem is that we found out this structure can only accept one row with NULL value. If we try to add second row with NULL value we get an error like. “can not insert duplicate key row in object….”

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

3
  • 1
    Drop the UNIQUE index. Commented Mar 4, 2013 at 16:19
  • 1
    Index's should never be null, why do you need it to be null? Commented Mar 4, 2013 at 16:19
  • 2
    @KevinA: What do you mean, if I have a column that is nullable, I should not have indexes on it? Why not? Commented Mar 4, 2013 at 16:22

1 Answer 1

30

Yes, you can use filtered index to support this. Just drop your existing index and create new index like this

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

This will allow you to have duplicates of NULL values. Here is an in depth article if you need more details on this.

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.