Five types of constraints for Oracle

Source: Internet
Author: User

1. Non-null (NOT NULL) constraint: The defined column is not absolutely null;

For example, modify the BookName field in the created table book to Be not empty:

Use ALTER TABLE .... MODIFY ..... Not NULL;

ALTER TABLE  not NULL;

2. Primary key (PRIMARY key) constraint: A unique identifier for each row in the table;

For example: Add a primary key named "BOOK_PK" for the BookID field in the Created table book

Use Alter TABLE ..... ADD CONSTRAINT book_pk PRIMARY KEY (field);

ALTER TABLE ADD CONSTRAINT PRIMARY KEY (BookID);

3. Unique constraint: The value of each column field cannot be the same;

Example: Add a unique constraint to the Phone field in table Usertable

Use Alter TABLE ..... ADD CONSTRAINT book_pk PRIMARY KEY (field);

ALTER TABLE ADD CONSTRAINT Unique (phone);

4. Foreign key (FOREIGN key) constraint: Used to maintain referential integrity between the table and the primary table;

For example, dept is the primary table, the EMP is from the table (foreign key table), and the foreign key column in the EMP deptno references the key in dept

ALTER TABLE ADD CONSTRAINT FOREIGN KEY REFERENCES Dept (DEPTNO);

Cascade Delete: When the referenced column in the reference table (primary table) is deleted, the corresponding data in the foreign key table is also deleted (implemented as follows)

ALTER TABLE ADD CONSTRAINT FOREIGN KEY REFERENCES  on DELETE CASCADE;

5. Condition (CHECK) constraint: The constraint is met for each row in the table.

For example, to create a given student table (Student), define a CHECK constraint (age>0 and age<120) for the Age column (ages) for the Disable state (some of the code when the table is built is as follows:)

int constraint Check (age>0 and age<disable),

Five types of constraints for 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.