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


UNIQUE constraint


UNIQUE constraint

Author
Message
Gopi S
Gopi S
SSC-Addicted
SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)SSC-Addicted (425 reputation)

Group: General Forum Members
Points: 425 Visits: 325
Comments posted to this topic are about the item UNIQUE constraint
Lokesh Vij
Lokesh Vij
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4430 Visits: 1599
I am sorry to say, looking at the correct answer, the question seems to be confusing! The question should have mentioned "Unique Index" instead of "Unique Constraint". Ideally, both Unique constraint and Unique index are same as Unique Constraint creates Unique Index to maintain the constraint to prevent duplicate keys. But eventually there are many differences and with the advent/introduction of Filtered Index, differences adds on...

After reading the question, the first thing that came to my mind was to think of creating a unique constraint like below:
ALTER TABLE Table_name
ADD CONSTRAINT Constraint_name UNIQUE (Column_name)



And this shall only allow one NULL value in the table!

~ 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


Yggaz
Yggaz
Old Hand
Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)

Group: General Forum Members
Points: 325 Visits: 189
Do not like the question.

First of all, it IS definitely possible to create a table which would accept any number of NULLs but no duplicate non-null values in 2005 and perphaps in 2000. You can use computed column which substitutes a key value when the value in question is null. In details it is here.

The problem lies in the words "unique constraint". It has at least TWO meanings: "something that does not allow duplicates" and "SQL Server object implemented via unique index". And this second meaning makes the question... unfair.

You are NOT to create the unique CONSTRAINT (as SQL Server object) that allows multiple nulls. Unique CONSTRAINT can only allow one null value. What you can do is to create a unique INDEX which will impose certain constraint (limitation) but will NOT create a SQL Server constraint.

If the question had been about INDEX, it would have been completely fair.

Nevertheless, thanks for the reminder about filtered index.
Yggaz
Yggaz
Old Hand
Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)

Group: General Forum Members
Points: 325 Visits: 189
Lokesh Vij (12/19/2012) Ideally, both Unique constraint and Unique index are same as Unique Constraint creates Unique Index to maintain the constraint to prevent duplicate keys.


They are definitely not. Every unique constraint has a corresponded unique index and operates via it. But the opposite is not true: you can have unique index without unique constraint.
Lokesh Vij
Lokesh Vij
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4430 Visits: 1599
Yggaz (12/19/2012)
Lokesh Vij (12/19/2012) Ideally, both Unique constraint and Unique index are same as Unique Constraint creates Unique Index to maintain the constraint to prevent duplicate keys.


They are definitely not. Every unique constraint has a corresponded unique index and operates via it. But the opposite is not true: you can have unique index without unique constraint.


Agreed!
I am trying to keep Qotd Author's perspective. As in "Ideal" case both Unique Constraint and Unique index helps us to yield same DB integrity rule - "Unique Records".

~ 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


Dineshbabu
Dineshbabu
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1610 Visits: 569
Today's question is completely misleading. Keeping unique constraint in mind which allows only one NULL value i answered worngly.

--
Dineshbabu
Desire to learn new things..
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)

Group: General Forum Members
Points: 99805 Visits: 13323
Got it wrong because the question specified unique constraint instead of unique index.
And even then, the unique index in the sample code doesn't allow NULLs. The index is defined on the part of the table where there aren't any NULLs. This would be the same as saying:

"There a night club in the city. The night club doesn't allow people under 21 to enter. Ergo, the whole city doesn't allow people under 21 to enter."


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
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24851 Visits: 7504
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)...

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)

Group: General Forum Members
Points: 99805 Visits: 13323
Stewart "Arturius" Campbell (12/20/2012)

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


Maybe, but my crystal ball is in the shop for repairs, so unless the OP mentions it, there's no need to assume it. :-)


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
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)

Group: General Forum Members
Points: 100194 Visits: 18616
Based on the question:
Is it possible to create a table with unique constraint that allows multiple NULL values from SQL Server 2008 onwards?


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.

Instead expand that constraint a little and the question is completely valid.


USE tempdb;
Go

CREATE TABLE sometest (testid INT, col1 INT, col2 INT);

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

INSERT INTO sometest(testid,col1,col2)
VALUES (NULL,1,1),(1,NULL,1),(NULL,NULL,NULL);

SELECT *
FROM sometest;

DROP TABLE sometest;




This demonstrates that multiple null values is possible and that multiple null values is even possible within each of the columns so long as all of the columns together remain unique.

That said, the sample code provided in the explanation for this question does work. It does allow two null values to be inserted. And it is also a form of creating a unique constraint.



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


SQL RNNR

Posting Performance Based Questions - Gail Shaw

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