A small test on the relationship between primary key creation and index creation

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.