question

sathishkumar avatar image
sathishkumar asked

Create Index

Hi, Im trying to create an index using IGNORE_DUP_KEY = ON in sql server 2000, when executing the query it showing syntax error. help me to correct the error.. query create unique index ind_dup_1 on dup_index(firstname) with (IGNORE_DUP_KEY = ON)
sql-server-2000index
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered
If you already have data in the table, with duplicate values, then the unique index cannot be created. To add this index to a table where duplicate values already exists, you will have to de-dupe the data first. The `IGNORE_DUP_KEY` option only controls the response when you subsequently try to insert duplicate values into the table. - ON : means that a warning message will be given and only the rows violating the unique constraint will fail to b inserted - OFF (the default) : means that the whole insert statement will fail and will be rolled back ---- Edit : follow the comments on this answer and we eventually get to the answer: create unique index ind_dup_1 on dup_index(firstname) with IGNORE_DUP_KEY
11 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
haven't got a 2000 instance available to me to test but can you try `create unique index ind_dup_1 on dup_index(firstname) with IGNORE_DUP_KEY`
2 Likes 2 ·
sathishkumar avatar image sathishkumar commented ·
I have created new table and tried to create index for that table..there is no values..
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
ahh I was guessing what the error was!
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
Haven't got a 2000 instance to test on, but try removing the brackets `create unique index ind_dup_1 on dup_index(firstname) with IGNORE_DUP_KEY = ON`
0 Likes 0 ·
sathishkumar avatar image sathishkumar commented ·
i would have tried removing brackets but it showing the same error.
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
can you post the table creation script too - I've just mocked this up on my machine and it works ok.
0 Likes 0 ·
sathishkumar avatar image sathishkumar commented ·
create table dup_index(id int,firstname varchar(10) ,lastname varchar(10), city varchar(10)) create unique index ind_dup_1 on dup_index(firstname) with IGNORE_DUP_KEY = ON
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
Forget my comment about removing the brackets - you need those. However `create table dup_index(id int,firstname varchar(10) ,lastname varchar(10), city varchar(10))` `create unique index ind_dup_1 on dup_index(firstname) with (IGNORE_DUP_KEY = ON)` works fine for me on a 2008 instance running under 2000 compatibility.
0 Likes 0 ·
sathishkumar avatar image sathishkumar commented ·
not working..this option is available in sql server 2000??
0 Likes 0 ·
sathishkumar avatar image sathishkumar commented ·
ya its working...thanks a lot kev riley
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
I think that's what I was trying to get to when I suggested removing the brackets
0 Likes 0 ·
Pavel Pawlowski avatar image
Pavel Pawlowski answered
On SQL 2000 it should be without the `=` create unique index ind_dup_1 on dup_index(firstname) with IGNORE_DUP_KEY ON Created a test table on SQL 2008, and generated script including the index with the script compatibility set to SQL 2000..
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image
Matt Whitfield answered
The 2000 syntax is different - it is create unique index ind_dup_1 on dup_index(firstname) with IGNORE_DUP_KEY No brackets, no =, no ON
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.