CHECK Constraints for Oracle

Source: Internet
Author: User

A check constraint is an inspection constraint, when you use a check constraint. Every data entered will be checked and only records that match the criteria will be saved to the table, guaranteeing the validity and completeness of the data.
Check constraints have the following four features:
In an expression of a check constraint, one or more columns in the table must be referenced, and the result of the expression's operation is a Boolean value.


In one of the listed species. Ability to define multiple check constraints.
For the same column, you can define a check constraint and a NOT NULL constraint at the same time.
A check constraint can be defined at the column level. can also be defined at the table level.
Over here. We created a P3 table as a demonstration:
Sql> CREATE TABLE P3 (
2 PID Number (4) NOT null unique,
3 pname varchar2 (10),
4 page number (3) Constraint check_p3age Check (page>0)-Specifies the check constraint for page, where page must be greater than 0
5);
The table is created.


Sql> INSERT INTO P3 values (1, ' AAA ', 20);
1 rows have been created.
Sql> INSERT INTO P3 values (1, ' AAA ', 0);
INSERT into P3 values (1, ' AAA ', 0)
*
An error occurred on line 1th:
ORA-02290: Violation of CHECK constraints (SYSTEM. Check_p3age)


Sql> ALTER TABLE p3 add constraint check2_p3age check (page<100); --add constraints to the table. Yes, the page must have a value less than 100
The table has changed.
Sql> INSERT INTO P3 values (1, ' AAA ', 100); --when inserting a value greater than or equal to 100 times wrong
INSERT into P3 values (1, ' AAA ', 100)
*
An error occurred on line 1th:
ORA-02290: Violation of CHECK constraints (SYSTEM. Check2_p3age)


Sql> ALTER TABLE p3 drop constraint check2_p3age; --Delete CHECK constraints in a table

The table has changed.

The above content is from the study record.


CHECK 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.