Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-10-24
6
Medium Priority
?
307 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 600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 600 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

886 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