Add primary keys and indexes in Oracle

Source: Internet
Author: User

The primary key and index of data are usually required. Especially when a table has a large amount of data, the index and primary key are even more necessary, which can provide data query efficiency;

I. Create a primary key constraint while creating a table

(1) No Name

Create table student (

Studentid int primary key not null,

Studentname varchar (8 ),

Age int );

(2) named

Create table students (

Studentid int,

Studentname varchar (8 ),

Age int,

Constraint yy primary key (studentid ));

2. Delete the existing primary key constraints in the table

(1) named

Alter table students drop constraint yy;

(2) No Name

SELECT * from user_cons_columns;

Find the primary key name in the student table. The primary key name is SYS_C002715.

Alter table student drop constraint SYS_C002715;

3. Add a primary key constraint to the table

Alter table student add constraint pk_student primary key (studentid );

3. When creating a primary key, the original primary key column contains data with duplicates. In this case, you must delete the duplicate data before creating it:

Delete from Tb_TEST a where rowid! = (Select max (rowid) from Tb_TEst B where a. test_ID = B. test_ID );

2. Create an index

Create Index IX_xxxx on table_name (colummname );

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.