For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!
Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.
That is expected behaviour. If an index on the primary key columns already exists, then adding the primary key constraint does not add a new index. Later when you drop the primary key, index is not dropped since it was not created as part of the add primary key constraint statement.
On the other hand, if you do not have a prior index on the primary key columns, a index is created as part of the add constraint primary key statement. In this case, dropping the constraint will drop the iindex as well.
Since you did an export and import, the import works by creating the index separate and then adding the primary key constraint separate. Since in case of import, the two thiings happen separately, dropping the primary key now will NOT drop the index.
Look at the below scenario, here no export import is involved, but in first case index is not dropped automatically:
SQL> create unique index pk_emp1 on emp1(empno) ; Index created. SQL> select index_name, table_name, uniqueness from user_indexes where table_name = 'EMP1' ; INDEX_NAME TABLE_NAME UNIQUENES ------------------------------ ------------------------------ --------- PK_EMP1 EMP1 UNIQUE 1 row selected. SQL> alter table emp1 add constraint pk_emp1 primary key(empno) ; Table altered. SQL> select index_name, table_name, uniqueness from user_indexes where table_name = 'EMP1' ; INDEX_NAME TABLE_NAME UNIQUENES ------------------------------ ------------------------------ --------- PK_EMP1 EMP1 UNIQUE 1 row selected. SQL> alter table emp1 drop primary key ; Table altered. SQL> select index_name, table_name, uniqueness from user_indexes where table_name = 'EMP1' ; INDEX_NAME TABLE_NAME UNIQUENES ------------------------------ ------------------------------ --------- PK_EMP1 EMP1 UNIQUE 1 row selected. SQL> SQL> drop index pk_emp1 ; Index dropped. SQL> select index_name, table_name, uniqueness from user_indexes where table_name = 'EMP1' ; no rows selected SQL> alter table emp1 add constraint pk_emp1 primary key(empno) ; Table altered. SQL> select index_name, table_name, uniqueness from user_indexes where table_name = 'EMP1' ; INDEX_NAME TABLE_NAME UNIQUENES ------------------------------ ------------------------------ --------- PK_EMP1 EMP1 UNIQUE 1 row selected. SQL> alter table emp1 drop primary key ; Table altered. SQL> select index_name, table_name, uniqueness from user_indexes where table_name = 'EMP1' ; no rows selected SQL> disconnect Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production With the Partitioning, OLAP and Data Mining options SQL>
Very strange.. My database was not a export and import and i am on 9.2.0.7
This is what my test produced.
SQL> create table test(a number,b number); Table created. SQL> create unique index test_i on test(a,b); Index created. SQL> alter table test add constraint test_i primary key (a,b) using index tablespace user_data; Table altered. SQL> select index_name from user_indexes where table_name='TEST'; INDEX_NAME ------------------------------ TEST_I SQL> alter table test drop primary key; Table altered. SQL> select index_name from user_indexes where table_name='TEST'; no rows selected /[PRE] G
create table test(a number,b number); Table created. SQL> create unique index test_i on test(a,b); Index created. SQL> alter table test add constraint test_i primary key (a,b) using index tablespace users; Table altered. SQL> select index_name from user_indexes where table_name='TEST'; INDEX_NAME ------------------------------ TEST_I SQL> alter table test drop primary key; Table altered. SQL> select index_name from user_indexes where table_name='TEST'; INDEX_NAME ------------------------------ TEST_I SQL>