UNIQUE constraint

  • Comments posted to this topic are about the item UNIQUE constraint

  • 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[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • 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.

  • 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.

  • 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[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • 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..

  • 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."

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • This was removed by the editor as SPAM

  • 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. 🙂

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • 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[/url]
    Learn Extended Events

  • 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.

  • 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[/url]
    Learn Extended Events

  • 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.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • 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

  • 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 😀

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

Viewing 15 posts - 1 through 15 (of 64 total)

You must be logged in to reply to this topic. Login to reply