SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


UNIQUE constraint


UNIQUE constraint

Author
Message
dawryn
dawryn
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3420 Visits: 914
Got it wrong for the same reason. I thought about unique filtered indexes but that is not the same as unique constraints which allow one null only.
SQLRNNR
SQLRNNR
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40811 Visits: 18565
Stewart "Arturius" Campbell (12/20/2012)
IMHO, I think the correct answer here will be "It depends".
It depends on whether filtered indexes are utilised or not, etc.

However, it could be that the PO intended that the use of filtered index(es) is implied (or should be assumed)...


I disagree that "It depends" with this question. It was asked if it was simply possible. No need to consider filtered indexes, you just need to know if there is any possible way to insert multiple nulls into a unique constraint - imho.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Koen Verbeeck
Koen Verbeeck
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34721 Visits: 13270
SQLRNNR (12/20/2012)
Stewart "Arturius" Campbell (12/20/2012)
IMHO, I think the correct answer here will be "It depends".
It depends on whether filtered indexes are utilised or not, etc.

However, it could be that the PO intended that the use of filtered index(es) is implied (or should be assumed)...


I disagree that "It depends" with this question. It was asked if it was simply possible. No need to consider filtered indexes, you just need to know if there is any possible way to insert multiple nulls into a unique constraint - imho.


A unique constraint is not the same as a unique index.

Anyway, your explanation of a constraint over multiple columns demonstrates that the question has the correct answer (albeit a little tricky :-)), but just an incorrect explanation.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Gopi S
Gopi S
SSC Veteran
SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)

Group: General Forum Members
Points: 243 Visits: 325
Hello,
"Data Integrity" can be enforced in sql server through
PRIMARY KEY Constraint
FOREIGN KEY Constraint
UNIQUE ConstraintCHECK Constraint
Default Definition
so on..

My point here is all about enforcing "Unique Constraint".
This can be done in several ways.

Query 1. Add Unique Constraint
ALTER TABLE dbo.<tablename> ADD CONSTRAINT
<constraint_name> UNIQUE NONCLUSTERED
(
<columnname>
) ON [PRIMARY]

Query 2. Add Unique Index
CREATE UNIQUE NONCLUSTERED INDEX
<index_name> ON dbo.<tablename>
(
<columnname>
) ON [PRIMARY]


Technically there is no difference between Unique Index and Unique Constraint. Even though syntax are different the effect is the same.
Unique Constraint creates Unique Index to maintain the constraint to prevent duplicate keys. Unique Index also creates index that are physical structure that maintain uniqueness. It is a convenient way to enforce a Unique Constraint for SQL Server.
In above case "Unique Constraint" allows one "NULL".

But my question was how the above Unique Constraint can be created but allowing multiple NULL values and not the RULES of Unique constraint.

I just want to bring that it is possible to enforce Uniqueness in a column but allowing multiple "NULL" values and one of the way to implement this is using filtered index.

Ref: http://blog.sqlauthority.com/2007/04/26/sql-server-difference-between-unique-index-vs-unique-constraint/
http://social.msdn.microsoft.com/Forums/en/transactsql/thread/a298b63b-e1eb-4b31-a2d7-64e1fe493b0a
Thanks
Gopi
Lokesh Vij
Lokesh Vij
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2456 Visits: 1599
SQLRNNR (12/20/2012)
Stewart "Arturius" Campbell (12/20/2012)
IMHO, I think the correct answer here will be "It depends".
It depends on whether filtered indexes are utilised or not, etc.

However, it could be that the PO intended that the use of filtered index(es) is implied (or should be assumed)...


I disagree that "It depends" with this question. It was asked if it was simply possible. No need to consider filtered indexes, you just need to know if there is any possible way to insert multiple nulls into a unique constraint - imho.


Let us all "Agree" to "Disagree" and come to a point that today's Question has a take-away on Filter Index :-D

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


dawryn
dawryn
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3420 Visits: 914
SQLRNNR (12/20/2012)
I don't disagree with the correct answer. I disagree with the explanation. I also do disagree with many of the complaints thus far about the question. You can most certainly have multiple null values in a unique constraint. Don't constrain yourselves to a narrow scope of a constraint on a single column - where you can have only one null value.


ALTER TABLE sometest
ADD CONSTRAINT someconstraint UNIQUE (testid,col1,col2);


Disagree!
This constraint can not represent value of Null even if all columns are populated with Nulls.
Besides, only one occurrence of Nulls in all columns is allowed.
SQLRNNR
SQLRNNR
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40811 Visits: 18565
dawryn (12/20/2012)
SQLRNNR (12/20/2012)
I don't disagree with the correct answer. I disagree with the explanation. I also do disagree with many of the complaints thus far about the question. You can most certainly have multiple null values in a unique constraint. Don't constrain yourselves to a narrow scope of a constraint on a single column - where you can have only one null value.

...
Besides, only one occurrence of Nulls in all fields is allowed.


True only one occurence of null in all fields is allowed. But that is not the question. The question was simply if multiple nulls could be inserted.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Gopi S
Gopi S
SSC Veteran
SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)SSC Veteran (243 reputation)

Group: General Forum Members
Points: 243 Visits: 325
So far Jason is following me :-P
Koen Verbeeck
Koen Verbeeck
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34721 Visits: 13270
Gopi S (12/20/2012)

Technically there is no difference between Unique Index and Unique Constraint. Even though syntax are different the effect is the same.
Unique Constraint creates Unique Index to maintain the constraint to prevent duplicate keys. Unique Index also creates index that are physical structure that maintain uniqueness. It is a convenient way to enforce a Unique Constraint for SQL Server.
In above case "Unique Constraint" allows one "NULL".


Technically, there's a difference. Logically yes, they achieve the same, but technically they are different. An index is not a constraint and vice versa.

I know the first hit on Google is the blog post of Pinal Dave who says they are the same, but if you look a bit further on the search results, you'll also find posts that contradict this. By the way, to avoid copyright infringement or plagiarism, you should include a reference to the blog of Pinal Dave, not just copy paste its contents and pretend it's yours.

Difference Between Unique Index vs Unique Constraint


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Koen Verbeeck
Koen Verbeeck
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34721 Visits: 13270
Gopi S (12/20/2012)
So far Jason is following me :-P


Yes, by saying your explanation is insufficient...

edit: I forgot to mention: thanks for the effort to create and submit a question. Hopefully you keep this up and hopefully there won't be any confusion on your next question


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search