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

Stairway to SQL Server Indexes: Step 8, Unique Indexes Expand / Collapse
Author
Message
Posted Wednesday, March 23, 2011 11:32 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, June 23, 2012 10:54 AM
Points: 8, Visits: 23
Comments posted to this topic are about the item Stairway to SQL Server Indexes: Step 8, Unique Indexes
Post #1082875
Posted Thursday, June 23, 2011 9:03 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, March 14, 2014 8:08 AM
Points: 267, Visits: 278
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.



Post #1130520
Posted Tuesday, July 26, 2011 4:49 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, February 13, 2013 3:31 AM
Points: 55, Visits: 39
"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.
Post #1148141
Posted Thursday, July 28, 2011 4:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, September 26, 2013 2:13 AM
Points: 1, Visits: 26
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
Post #1149961
Posted Wednesday, September 14, 2011 7:44 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 6:35 AM
Points: 1,251, Visits: 1,596
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.
Post #1174917
Posted Wednesday, September 14, 2011 8:15 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, April 20, 2014 5:18 PM
Points: 424, Visits: 1,544
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'."
Post #1174944
Posted Wednesday, September 14, 2011 10:27 AM


SSC Eights!

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

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 10:36 AM
Points: 896, Visits: 1,479
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
Post #1175100
Posted Wednesday, September 14, 2011 11:06 AM


SSC Eights!

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

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 10:36 AM
Points: 896, Visits: 1,479
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
Post #1175128
Posted Friday, January 06, 2012 3:19 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, January 23, 2013 9:53 AM
Points: 6, 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
Post #1231761
Posted Wednesday, April 11, 2012 12:55 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, December 28, 2012 1:42 PM
Points: 36, Visits: 30
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]


Post #1281887
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse