Creation of primary key, foreign key, index, sequence, uniqueness constraint in Oracle

Source: Internet
Author: User
Tags create index

1, the creation of the primary key

Method One: Declare the field PRIMARY KEY constraint directly in the SQL statement

CREATE TABLE table_name (ID type[length] constraint pk_name primary key,name tyoe[length],age type[length],class_id);

Method two: Alter change table ADD constraint

ALTER TABLE TABLE_NAME ADD constraint Pk_name primary key (field);

Delete:

ALTER TABLE table_name DROP constraint contraint_name;

Rename:

ALTER TABLE TABLE_NAME rename constraint old_name to new_name;

Failure:

ALTER TABLE TABLE_NAME MODIFY constraint constraint_name disable;

Effect:

ALTER TABLE TABLE_NAME MODIFY constraint constraint_name enable;

2. Creation of foreign keys

Method One: Declare directly in the SQL statement

CREATE TABLE table_name (ID type[length] PRIMARY key table_name_pk,name Tyoe[length],age Type[length],

Class_id,foreign Key (class_id) references Table2_name (class_id));//field type to be consistent

Method two: Alter change table ADD constraint

ALTER TABLE TABLE_NAME ADD constraint fk_name foreign key (class_id) references Table2_name (class_id);

Delete:

ALTER TABLE table_name DROP CONSTRAINT constraint_name;

Rename:

ALTER TABLE TABLE_NAME rename constraint old_name to new_name;

Failure:

ALTER TABLE TABLE_NAME MODIFY constraint constraint_name disable;

Effect:

ALTER TABLE TABLE_NAME MODIFY constraint constraint_name enable;

3, the creation of the index

CREATE INDEX index_name on table_name (field);

Delete: Drop index index_name;

Rename: Alter index index_name rename to new_name;

Effect:

Failure:

4. Creation of sequences

Create sequence Sequence_name

Start with 1

Increment by 1

Nomaxvalue

Nocycle;

Delete: Drop sequence sequence_name;

5. Creation of uniqueness constraints

CREATE TABLE table_name (ID type[length],name type[length],

Num type[length] Constraint constraint_name unique,class_id type[length]);

ALTER TABLE TABLE_NAME ADD constraint constraint_name unique (one or more fields);

Creation of primary key, foreign key, index, sequence, uniqueness constraint in Oracle

Related Article

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.