Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

UNIQUE constraint Expand / Collapse
Author
Message
Posted Wednesday, December 19, 2012 8:18 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 1:05 PM
Points: 157, Visits: 292
Comments posted to this topic are about the item UNIQUE constraint
Post #1398745
Posted Wednesday, December 19, 2012 9:21 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 6:38 PM
Points: 1,371, Visits: 1,560
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

Post #1398758
Posted Wednesday, December 19, 2012 9:24 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 3:12 AM
Points: 207, Visits: 167
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.
Post #1398760
Posted Wednesday, December 19, 2012 9:29 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 3:12 AM
Points: 207, Visits: 167
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.
Post #1398764
Posted Wednesday, December 19, 2012 9:38 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 6:38 PM
Points: 1,371, Visits: 1,560
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

Post #1398766
Posted Wednesday, December 19, 2012 9:59 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, August 4, 2014 5:38 AM
Points: 945, Visits: 558
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..
Post #1398771
Posted Thursday, December 20, 2012 12:16 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:29 PM
Points: 12,966, Visits: 10,742
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1398815
Posted Thursday, December 20, 2012 12:19 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 8:45 AM
Points: 3,969, Visits: 5,212
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”
Post #1398817
Posted Thursday, December 20, 2012 12:27 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:29 PM
Points: 12,966, Visits: 10,742
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1398823
Posted Thursday, December 20, 2012 12:33 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:15 PM
Points: 17,654, Visits: 15,506
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1398825
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse