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


Stairway to SQL Server Indexes: Step 8, Unique Indexes


Stairway to SQL Server Indexes: Step 8, Unique Indexes

Author
Message
David Durant
David Durant
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 23
Comments posted to this topic are about the item Stairway to SQL Server Indexes: Step 8, Unique Indexes
pnewhart
pnewhart
Old Hand
Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)

Group: General Forum Members
Points: 315 Visits: 386
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
simonharpham
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 43
"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
vas1l
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 30
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 Sad
sknox
sknox
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2624 Visits: 2832
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
Yuri55
Say Hey Kid
Say Hey Kid (690 reputation)Say Hey Kid (690 reputation)Say Hey Kid (690 reputation)Say Hey Kid (690 reputation)Say Hey Kid (690 reputation)Say Hey Kid (690 reputation)Say Hey Kid (690 reputation)Say Hey Kid (690 reputation)

Group: General Forum Members
Points: 690 Visits: 2025
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
Andre Guerreiro
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1349 Visits: 1515
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
Andre Guerreiro
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1349 Visits: 1515
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
mmilodragovich
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 39
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
noviceinsql
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 46

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]



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