Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Constraint question 2

Status
Not open for further replies.

djj55

Programmer
Feb 6, 2006
1,761
US
Hello, SQL 2008 R2
Is it possible to have a constraint that allows NULL but not duplicate values? Sort of like a unique index but allowing multiple NULLs.

Thank you,

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
I encourage you to play around with this a bit before you implement in production!

With SQL2008R2, you can create a filtered unique constraint. All you need to do is filter out the nulls, like this...

Code:
CREATE UNIQUE NONCLUSTERED INDEX idx_YourIndexNameHere
ON YourTableNameHere(YourColumnNameHere)
WHERE YourColumnNameHere IS NOT NULL



-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thank you both. Great answers.

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top