473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Using Unique Indexes

Bob
Why, in the process of creating a unique index, does SQL Server allow
me to select the "Ignore duplicate keys" option? Wouldn't I just
create a non-unique index if I wanted to ignore duplicate keys? I
came across this fact while preparing for the SQL Server design exam.
Jul 20 '05 #1
3 1580
The IGNORE_DUP_KEY option on a unique index defines what happens when you
attempt to insert a row with a duplicate key.

With or without this option duplicate rows are of course not permitted in
unique indexes. Without the IGNORE_DUP_KEY option the entire INSERT will
fail. With IGNORE_DUP_KEY only the duplicated row(s) will fail.

With IGNORE_DUP_KEY, if you insert more than one duplicate row in a single
insert statement then one of them will be inserted (provided a duplicate row
doesn't already exist in the table) and the rest will be ignored. In that
situation you cannot control which row will be inserted and which
duplicate(s) will be ignored.

Because the behaviour of this option is so peculiar and non-standard I
recommend you avoid it. I can't think of a good reason for using this option
at all. If you must use it then do so with care. When writing code that
INSERTs to a table indexed in that way you need to consider the possible
implications for data integrity if only part of your data is added to the
table. In my view it's a lot less trouble to write code that doesn't insert
duplicates in the first place and to do without IGNORE_DUP_KEY.

Finally, a unique *constraint* is normally preferred over a unique *index*
and unique constraints don't allow this option.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
David,

I've used this, albeit rarely, for tables that get their data from
sloppy sources that include duplicates and near duplicates. If lots
of junk comes in, and I just need one row of the junk for each (you
name the key), this does the trick when yelling at the provider doesn't.

Steve Kass
Drew University

David Portas wrote:
The IGNORE_DUP_KEY option on a unique index defines what happens when you
attempt to insert a row with a duplicate key.

With or without this option duplicate rows are of course not permitted in
unique indexes. Without the IGNORE_DUP_KEY option the entire INSERT will
fail. With IGNORE_DUP_KEY only the duplicated row(s) will fail.

With IGNORE_DUP_KEY, if you insert more than one duplicate row in a single
insert statement then one of them will be inserted (provided a duplicate row
doesn't already exist in the table) and the rest will be ignored. In that
situation you cannot control which row will be inserted and which
duplicate(s) will be ignored.

Because the behaviour of this option is so peculiar and non-standard I
recommend you avoid it. I can't think of a good reason for using this option
at all. If you must use it then do so with care. When writing code that
INSERTs to a table indexed in that way you need to consider the possible
implications for data integrity if only part of your data is added to the
table. In my view it's a lot less trouble to write code that doesn't insert
duplicates in the first place and to do without IGNORE_DUP_KEY.

Finally, a unique *constraint* is normally preferred over a unique *index*
and unique constraints don't allow this option.

Jul 20 '05 #3
Yes, I suppose that's one valid application of IGNORE_DUP_KEY, although you
then have to deal with the non-atomic nature of inserts if you have other
processes updating the same table. In my environment I usually prefer to do
that sort of data-cleansing using third-party ETL tools.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Laphan | last post by:
Having re-jigged the UNIQUE to the proper syntax it does do what I want - wahey!! Just to finish this off here is my final table DDL: CREATE TABLE `WEBSTRINGS` ( `STRINGID` INT NOT NULL...
2
by: reneeccwest | last post by:
Hello, I plan to create a table with 3 unique keys. Combination of three fields has to be unique for each row in a table that are vendor ID (char 8), vendor name (char 40), and vendor...
2
by: Mansoor Azam | last post by:
When I add a unique key constraint to column in SQL 6.5 why does it also create an index. e.g. In the table subaccounts I added a unique key constraint for the column login and SQL creates an index...
4
by: Q. John Chen | last post by:
All, What's the difference between a unique contraint and unique? sementically, if you want a column contain unique values, it is a contraint. And an index is for searching/sort. The questions...
10
by: BuddhaBuddy | last post by:
Platform is DB2/NT 7.2.9 The table was created like this: CREATE TABLE MYTEST ( MYTESTOID bigint not null primary key, FK_OTHEROID bigint not null references other, FK_ANOTHEROID bigint not...
8
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE(...
8
by: Paul Hunter | last post by:
I am new to databases and thus to Access. I have a situation where I am trying to figure out how to key some tables I am working with. Consider that I have a database of my own records which are...
4
by: p175 | last post by:
What are the advantages / disadvantages of having primary keys in lieu of ordinary indexes ? If there are no foreign relationships defined, is it better to have a two column index allowing reverse...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.