Solved

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

Posted on 2006-10-24
6
301 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:Richard Quadling
  • 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:Richard Quadling
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 40

Author Comment

by:Richard Quadling
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:Richard Quadling
ID: 17801535
That's great. Confirming some of the bits of knowledge I had.

Ta!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL - format decimal in a string 5 38
Row insertion failed. Array 5 45
SQL Script to Remove Data from Two Joined Tables 1 19
Powershell v3 - SQLCMD 3 26
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

790 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