Stairway to SQL Server Indexes: Step 8, Unique Indexes

  • David Durant

    Ten Centuries

    Points: 1370

    Comments posted to this topic are about the item Stairway to SQL Server Indexes: Step 8, Unique Indexes

  • pnewhart

    SSC Eights!

    Points: 953

    Great series so far. However, I think things are starting to blur in my mind.

    First, minor grammatical point: The IGNORE_DUP_KEY option only effects INSERT statements. This should be affects. 🙂

    Second, how are you demonstrating to allow duplicate NULL values using a filter? Unless some screen prints are missing, I don't see how that's being illustrated. And, unless I'm misunderstanding the functionality of the IGNORE DUP_KEY=ON, this would still only allow one NULL value since it's defined as allowing the inserts to only fail those having duplicate values.

    Again, maybe I'm just missing something. Unfortunately, I'm just a lowly contract developer looking to fill some downtime, so I don't have the ability to add the AW db; therefore I can't try to duplicate the demos.

    Thanks.

  • simonharpham

    SSC Enthusiast

    Points: 139

    "The best way to provide this functionality is to combine a unique index with a filtered index."

    It looks like you forgot (deliberately?) to include the code to create this index within the body of the article.

  • vas1l

    Valued Member

    Points: 53

    First articles were very intelligible.

    Now it's hard to understand all nuances without good examples. Step 8 does not provide all sqls.

    The same problem peculiar to article #7.

    Seems that author just tired and decided to save time 🙁

  • sknox

    SSChampion

    Points: 12292

    pnewhart (6/23/2011)


    Second, how are you demonstrating to allow duplicate NULL values using a filter? Unless some screen prints are missing, I don't see how that's being illustrated. And, unless I'm misunderstanding the functionality of the IGNORE DUP_KEY=ON, this would still only allow one NULL value since it's defined as allowing the inserts to only fail those having duplicate values.

    Seconded. If you look closely at the article section on allowing duplicate NULLs, you see a reference to an index AK_UPCode, which is not created in the preceding DDL statements. So something is clearly missing there. Compounding this is the fact that the header for choosing the correct IGNORE DUP_KEY option is not formatted as a header, so the two sections run into each other.

  • Yuri55

    SSCrazy Eights

    Points: 8440

    pnewhart (6/23/2011)


    First, minor grammatical point: The IGNORE_DUP_KEY option only effects INSERT statements. This should be affects. 🙂

    IMO next statement from the article

    It is ignored by UPDATE, CREATE INDEX, and ALTER INDEX statements :

    is not correct as IGNORE_DUP_KEY does affect UPDATE statements-

    try it and you will get error "Cannot insert duplicate key row in object 'dbo.mytbl' with unique index 'nc_index1'."

  • Andre Guerreiro

    SSCertifiable

    Points: 7319

    I have seen that technique of combining unique indexes with filtered indexes before and it's really great to be able to do that. Thanks for bringing it up.

    I really like how you explain many questions at once in your article. That unique constraint VS unique indexes part was one of them.

    Best regards,

    Best regards,

    Andre Guerreiro Neto

    Database Analyst
    http://www.softplan.com.br
    MCITPx1/MCTSx2/MCSE/MCSA

  • Andre Guerreiro

    SSCertifiable

    Points: 7319

    A code showing a unique filtered index would be like this:

    CREATE UNIQUE NONCLUSTERED INDEX UX_TableName_Field ON dbo.TableName(Field)

    WHERE Field IS NOT NULL;

    Best regards,

    Best regards,

    Andre Guerreiro Neto

    Database Analyst
    http://www.softplan.com.br
    MCITPx1/MCTSx2/MCSE/MCSA

  • mmilodragovich

    SSC Veteran

    Points: 282

    Quick question regarding ignore_dup_key option:

    If the attempted insert includes multiple "duplicates", how do you control which one succeeds.

    From trial and error, it looks like if my set of duplicates is ordered, the first row will be successful and the subsequent rows will fail. (See test 2 and test 3 below.) But it would sure be nice if someone could confirm that this is the defined behavior. Anyone? Thanks.

    create table ItemTest1

    ( ItemNum int

    ,Value varchar(10))

    go

    create unique index pk_ItemTest1 on ItemTest1(ItemNum) with (IGNORE_DUP_KEY = ON)

    go

    --test 1

    insert into ItemTest1

    select 1,'row1'

    union all

    select 1,'row2'

    union all

    select 1,'row3'

    select * from ItemTest1

    where ItemNum=1

    --test 2

    insert into ItemTest1

    select 2,'row1'

    union all

    select 2,'row2'

    union all

    select 2,'row3'

    order by 2

    select * from ItemTest1

    where ItemNum=2

    --test 3

    insert into ItemTest1

    select 3,'row1'

    union all

    select 3,'row2'

    union all

    select 3,'row3'

    order by 2 desc

    select * from ItemTest1

    where ItemNum=3

    drop table ItemTest1

  • noviceinsql

    SSC-Addicted

    Points: 428

    SELECT [Name]

    , COUNT(*) AS 'RowCount'

    , SUM(LineTotal) AS 'TotalValue'

    FROM Production.Product P

    JOIN Sales.SalesOrderDetail D ON D.ProductID = P.ProductID

    GROUP BY ProductID

    should be

    SELECT [Name]

    , COUNT(*) AS 'RowCount'

    , SUM(LineTotal) AS 'TotalValue'

    FROM Production.Product P

    JOIN Sales.SalesOrderDetail D ON D.ProductID = P.ProductID

    GROUP BY [Name]

Viewing 10 posts - 1 through 10 (of 10 total)

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