Skip to Main Content
  • Questions
  • How to create a primary key constraint from a unique constraint without drop/recreate index

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Feng.

Asked: January 03, 2008 - 6:58 pm UTC

Last updated: January 03, 2008 - 7:13 pm UTC

Version: 9.2.8

Viewed 1000+ times

You Asked

Tom,

Happy New Year. I have a simple question but I couldn't find answer anywhere. I got ora-2261 when I tried to create a primary key constraint on one column which already has a unique constraint.

local@locbetar> create table a ( a number, constraint uk_a unique(a));

Table created.

Elapsed: 00:00:00.06
local@locbetar> alter table a add constraint pk_a primary key (a) using index;
alter table a add constraint pk_a primary key (a) using index
                                  *
ERROR at line 1:
ORA-02261: such unique or primary key already exists in the table


Elapsed: 00:00:00.00
local@locbetar> alter table a add constraint pk_a primary key (a);
alter table a add constraint pk_a primary key (a)
                                  *
ERROR at line 1:
ORA-02261: such unique or primary key already exists in the table


Elapsed: 00:00:00.01
local@locbetar> alter table a add constraint pk_a primary key (a) enable novalidate;
alter table a add constraint pk_a primary key (a) enable novalidate
                                  *
ERROR at line 1:
ORA-02261: such unique or primary key already exists in the table


Elapsed: 00:00:00.00
local@locbetar> alter table a add constraint pk_a primary key (a) using index uk_a;
alter table a add constraint pk_a primary key (a) using index uk_a
                                  *
ERROR at line 1:
ORA-02261: such unique or primary key already exists in the table


Is there any way I can create primary key constraint without drop/recreate the index on column a since my table has over 50+ million rows in real world.

Thanks

Feng


and Tom said...

ops$tkyte%ORA10GR2> create table t ( x int, y int constraint y_unique unique );

Table created.

ops$tkyte%ORA10GR2> insert into t select rownum, rownum from all_users;

59 rows created.

ops$tkyte%ORA10GR2> commit;

Commit complete.

ops$tkyte%ORA10GR2> select table_name, index_name from user_indexes where table_name = 'T';

TABLE_NAME                     INDEX_NAME
------------------------------ ------------------------------
T                              Y_UNIQUE

ops$tkyte%ORA10GR2><b> alter table t drop constraint y_unique KEEP INDEX;</b>

Table altered.

ops$tkyte%ORA10GR2> select table_name, index_name from user_indexes where table_name = 'T';

TABLE_NAME                     INDEX_NAME
------------------------------ ------------------------------
T                              Y_UNIQUE

ops$tkyte%ORA10GR2> alter table t add constraint t_primary_key primary key(y);

Table altered.

ops$tkyte%ORA10GR2> select table_name, index_name from user_indexes where table_name = 'T';

TABLE_NAME                     INDEX_NAME
------------------------------ ------------------------------
T                              Y_UNIQUE

ops$tkyte%ORA10GR2> drop index y_unique;
drop index y_unique
           *
ERROR at line 1:
ORA-02429: cannot drop index used for enforcement of unique/primary key

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Great! Thanks!

Feng Qu, January 03, 2008 - 8:00 pm UTC

Thanks for prompt response.