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?
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.
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.
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.
ALTER TABLE [TableName] REBUILD WITH (IGNORE_DUP_KEY = ON)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With