Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I am using SQL Server 2005. I want to constrain the values in a column to be unique while allowing NULLS.

My current solution involves a unique index on a view like so:

CREATE VIEW vw_unq WITH SCHEMABINDING AS

    SELECT Column1

      FROM MyTable

     WHERE Column1 IS NOT NULL

CREATE UNIQUE CLUSTERED INDEX unq_idx ON vw_unq (Column1)

Any better ideas?

1 Answer

0 votes
by (40.7k points)

If you are using SQL Server 2008, then create a filtered index.

 For detailed information refer to this: http://msdn.microsoft.com/en-us/library/cc280372.aspx

Another solution is to use a trigger to check uniqueness, but this may affect the performance.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...