When a primary key is created, if the corresponding column does not have an index, the database will automatically create an index by default. If the column has an index, the primary key will not be created again. Note that the column value must meet the primary key requirements (unique, non-empty). A simple test is as follows: SQLcreatetablewxlun_pri (anumber); Tablecreated. SQLaltert
When a primary key is created, if the corresponding column does not have an index, the database will automatically create an index by default. If the column has an index, the primary key will not be created again. Note that the column value must meet the primary key requirements (unique, non-empty). a simple test is as follows: SQL create table wxlun_pri (a number); Table created. SQL alter t
When a primary key is created, if the corresponding column does not have an index, the database will automatically create an index by default. If the column has an index, the primary key will not be created again. Note that the column value must meet the primary key requirements (unique, non-empty). A simple test is as follows:
SQL> create table wxlun_pri (a number );
Table created.
SQL> alter table wxlun_pri add constraint PRIMARY_KEY_REPLY primary key (a); ------ create a primary key constraint
Table altered.
SQL> select index_name from user_indexes where table_name = 'wxlun _ PRI '; ------ index generated by default
INDEX_NAME
------------------------------
PRIMARY_KEY_REPLY
SQL> alter table wxlun_pri drop constraint PRIMARY_KEY_REPLY;
Table altered.
SQL> select index_name from user_indexes where table_name = 'wxlun _ PRI ';
No rows selected
SQL> create index idx_wxlun_pri on wxlun_pri (a); ------ create a general index and add a primary key constraint when duplicate values exist in the test.
Index created.
SQL> insert into wxlun_pri values (1 );
1 row created.
SQL>/
1 row created.
SQL> commit;
Commit complete.
SQL> select * from wxlun_pri;
A
----------
1
1
SQL> alter table wxlun_pri add constraint PRIMARY_KEY_REPLY primary key (a); ------ the original index has duplicate values and the primary key constraint cannot be added.
Alter table wxlun_pri add constraint PRIMARY_KEY_REPLY primary key ()
*
ERROR at line 1:
ORA-02437: cannot validate (WXLUN. PRIMARY_KEY_REPLY)-primary key violated
Delete duplicate values and add primary key constraints
SQL> delete from wxlun_pri where rownum <2;
1 row deleted.
SQL> commit;
Commit complete.
SQL> select * from wxlun_pri;
A
----------
1
SQL> alter table wxlun_pri add constraint PRIMARY_KEY_REPLY primary key (a); ------ successful
Table altered.
SQL> select index_name from user_indexes where table_name = 'wxlun _ PRI ';
INDEX_NAME
------------------------------
IDX_WXLUN_PRI
SQL> drop table wxlun_pri;
Table dropped.
SQL>