Solved

Confusion about documentation in BOL with regard to "Ignore duplicate key".

Posted on 2006-10-24
6
299 Views
Last Modified: 2008-01-09
Hi.

BOL | Using the SQL Server Tools | User Interface Reference | Visual Database Tools | Properties Page | Database Designer Properties Page | Index/Keys Property Page says ...

[snip]
Create UNIQUE
Select this option to create a unique constraint or index for the selected database table. Specify whether you are creating a constraint or index by selecting either the Constraint or Index button.

Ignore duplicate key   If you create a unique index, you can set this option to ensure each value in an indexed column is unique.
[snip]

What will happen if you 2 rows with the same value for the key column(s)?

0
Comment
Question by:RQuadling
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17795526
the second row will not be added
0
 
LVL 40

Author Comment

by:RQuadling
ID: 17795538
How do I prove that? Is there an index dump facility?
0
 
LVL 10

Accepted Solution

by:
AaronAbend earned 150 total points
ID: 17795612
Insert a dup value - you do not need to see the index - just look at the data in the table...

create table ab(a int )
create unique index adbab on ab(a)   WITH( IGNORE_DUP_KEY = ON)

insert ab select 1
insert ab select 1

... messages:

(1 row(s) affected)

Duplicate key was ignored.
(0 row(s) affected)

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 40

Author Comment

by:RQuadling
ID: 17796108
Ah. So the index restricts the insertion of data. What about adding the index to existing data?

(More points as the discussion has wandered a little).
0
 
LVL 10

Assisted Solution

by:AaronAbend
AaronAbend earned 150 total points
ID: 17796532
No.. a unique index normally prevents a duplicate value from being entered by issuing an error message. But ignore_dup_key causes that message to go away - but it does not let a duplicate into the table.

If you have an existing table, you can create an index...


create unique index myindexname on mytable(mycol1, mycol2, ...)

and if there are values that duplicate the index build will fail.

In SQL we do not look at indexes directly - they are stored in the database and they are used by the optimizer to speed up queries. You can see which indexes are being used by showing the execution plan in a query analyzer.

A clustered index actually resorts the table itself - so in effect you are looking at the index when you look at data in a table that has a clustered index, but that's not a very important thing to know.

0
 
LVL 40

Author Comment

by:RQuadling
ID: 17801535
That's great. Confirming some of the bits of knowledge I had.

Ta!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now