Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set ignore_dup_key on for a primary key?

I have a two-column primary key on a table. I have attempted to alter it to set the ignore_dup_key to on with this command:

ALTER INDEX PK_mypk on MyTable SET (IGNORE_DUP_KEY = ON); 

But I get this error:

Cannot use index option ignore_dup_key to alter index 'PK_mypk' as it enforces a primary or unique constraint.

How else should I set IGNORE_DUP_KEY to on?

like image 283
Mr. Flibble Avatar asked Apr 07 '10 16:04

Mr. Flibble


People also ask

How do I set the Ignore DUP key?

ignore_dup_key is useful feature while you are having a unique index. In order to ensure the uniqueness of an index key and also guarantee your data insertion to be successful, you set the ignore_dup_key on. after you set the ignore_dup_key on if your importing data have duplicated keys it will be ignored.

What is Ignore_dup_key?

IGNORE_DUP_KEY = { ON | OFF } Specifies the response type when an insert operation attempts to insert duplicate key values into a unique index. The IGNORE_DUP_KEY option applies only to insert operations after the index is created or rebuilt. The option has no effect when executing CREATE INDEX, ALTER INDEX, or UPDATE.

How do I ignore a primary key in SQL?

You need to know the constraint name. In other words, you can't ignore a primary key. It is defined as unique and not-null. If you want the table to have duplicates, then that is not the primary key.


2 Answers

ALTER TABLE [TableName] REBUILD WITH (IGNORE_DUP_KEY = ON) 
like image 61
Kvasi Avatar answered Sep 21 '22 23:09

Kvasi


It's not documented in Books Online, but I've found that while IGNORE_DUP_KEY is valid for Primary Keys, you can't change it with an ALTER INDEX; you'll have to drop and re-create the primary key.

Keep in mind that IGNORE_DUP_KEY doesn't allow you to actually store duplicate rows in a unique index, it simply changes how it fails when you try it:

ON: A warning message will occur when duplicate key values are inserted into a unique index. Only the rows violating the uniqueness constraint will fail.

OFF: An error message will occur when duplicate key values are inserted into a unique index. The entire INSERT operation will be rolled back.

From http://msdn.microsoft.com/en-us/library/ms175132.aspx

like image 24
BradC Avatar answered Sep 22 '22 23:09

BradC