2

I want to change a primary key/index on a database table to change the index option ignore_dup_key to 'on'.

According to this question " Can I set ignore_dup_key on for a primary key? " I need to drop the index and create a new one.

Before I drop the index, how can I get the command to recreate it? Then I'll just change the ignore_dup_key option.

Community
  • 1
  • 1
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • Right click the primary key in SSMS, and choose Script -> As Create -> To New Window – Andomar May 15 '12 at 11:20
  • Thanks Andomar. Post that as an answer, I'll accept it. – Colonel Panic May 15 '12 at 11:28
  • Get the script from source control, you do have all your database objects and code under source control, right? – Pondlife May 15 '12 at 11:28
  • 3
    The disciple approached his master and spake thusly: "Master, I have a primary key upon my table, and I want to ignore duplicates thereupon. How may I accomplish this?" At this the master smiled gently and said "A donut...without a hole...is a danish". – Bob Jarvis - Слава Україні May 15 '12 at 11:33
  • Pondlife, the primary key was created along with the table `create table .. primary key`. I have that query under source control, but it's no good if I want to keep the table and change only the key. – Colonel Panic May 15 '12 at 11:34

4 Answers4

2

Right click the primary key in SSMS, and choose Script -> As Create -> To New Window

Andomar
  • 232,371
  • 49
  • 380
  • 404
1

As indicated by a commenter on another related question, you can enable IGNORE_DUP_KEY for the table:

ALTER TABLE [TableName] REBUILD WITH (IGNORE_DUP_KEY = ON);

and revert:

ALTER TABLE [TableName] REBUILD WITH (IGNORE_DUP_KEY = OFF);
Community
  • 1
  • 1
mcNux
  • 1,472
  • 1
  • 15
  • 13
0

I did not think SQL would allow a PK with IGNORE_DUP_KEY = on so I tested on SQL 2008 R2.
Via script could create a table with PK IGNORE_DUP_KEY = on. But even with drop and create could not change a PK from off to on. What is interesting is the script ran with no error but it did not change the PK from off to on. You may find different results in your environment.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
0

To Drop the Index You can run

DROP INDEX [IndexName] ON [TableName]

To Create the index with Ignore_Dup_Key option

CREATE UNIQUE INDEX [IndexName] ON [TableName]([ColumnNam])
WITH (IGNORE_DUP_KEY = ON)
Turbot
  • 5,095
  • 1
  • 22
  • 30