EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Oracle Tutorial Oracle Check Constraint
 

Oracle Check Constraint

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

Oracle Check Constraint

 

 

Introduction to Oracle Check Constraint

An Oracle Check Constraint is used to define an integrity constraint condition which restricts the value in each row in a Database Table. The column of the table which contains Check Constraint enforces the condition for every single row and restricts the value which is not declared in Check Constraint. In short each row in the table must make the Check Constraint condition either True or Unknown.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Declaration Style of Oracle Check Constraint

It can be declared in two ways. They are

  • Column Level (In-Line) style:
  • It can be declared as part of the definition of an individual column or attribute.
  • Usually applied when the constraint is specific to that column only.
  • Table Level (Out of Line) style:
  • It can be declared as part of the table definition.
  • It can be declared on a combination of columns together.

Points of Concentration

  • Oracle CHECK Constraint defines a condition each row must satisfy.
  • To satisfy the constraint, each row in the table must make the condition either TRUE or UNKNOWN.
  • Oracle does not verify that Check conditions are mutually exclusive.
  • Within a single user or schema no two Check constraints can have the same name.
  • If the user doesn’t provide a constraint name ORACLE associates the constraint with the name.

It’s better to provide a constraint name.

Restrictions

  • The constructs that cannot be included, are:
  • Queries to refer to values in other rows.
  • Calls to functions SYSDATE, UID, USERand USERENV.
  • The pseudo columns CURRVAL, NEXTVAL, LEVEL, or ROWNUM.
  • DATE constant that is not fully specified.
  • The condition of a Check Constraint can refer to any column in the same table, but it cannot refer to columns of another table (s).
  • A single column can have multiple Check Constraints that can reference the column in the definition.
  • There is no limit to the number of Check constraints that can be defined on a column.
  • The CHECK Constraint can be defined at the column level (In-Line) or Table level (Out of Line).

Syntax

For Inline Oracle Check Constraint:

CREATE TABLETable_Name(col_1 Datatype (width), col_2 Data type (width)   CONSTRAINT constraint_name CHECK (column_name Condition) [DISABLE]);

For Out of Line Oracle Check Constraint:

CREATE TABLETable_Name(col_1 Datatype (width), col_2 Datatype (width), col_2     Datatype (width), CONSTRAINT constraint_nameCHECK(column_name Condition) [DISABLE]);

Explanation:

  • Col_1/2/n: The column(s) or calculation as per your requirement.
  • Table_Name: As per your requirement
  • constraint_name: It can be any name but unique in the same schema.
  • column_name: It can be any column name but it must be in the same table on which condition will be applied.
  • Condition: Condition that will apply to the column.
  • DISABLE: It is an optional keyword. If any Check Constraint gets created using the DISABLE keyword that constraint will be created but the condition won’t get enforced.

Examples to Implement Oracle Check Constraint

In this section, we’ll see the Implementation of Oracle Check Constraint and its behavior.

1. Check Constraint / Column Level (In-Line) style

Code:

SQL> CREATE TABLE Mydept(Deptno NUMBER(2) CONSTRAINT Dep_chk CHECK(Deptno in(10,20,30,40)),Dname VARCHAR(16));

Output:

Create Statement

Explanation: In the above CREATE statement creates a table with two columns in that Deptno column contains CHECK constraint with a condition to check the Deptno belongs to the given number in the specified condition. Now we will insert some data in the table and see the behavior.

Code:

SQL>INSERT INTO Mydept VALUES (30,’SALES’);

Output:

Row Created

Code:

SQL>INSERT INTO Mydept VALUES (50,’SALES’);

Output:

Check Constraint

Explanation: The above two INSERT query first statement inserts a record in the table (Mydept)but the second statement throws an error.WHY? Because whenever runs a query against the column, Check Constraint enforces the condition. In the first example Deptno value is 30 which is satisfying the Check Constraint condition but in the second example Deptno value is 50 which is violating the Check Constraint condition. That is why the second INSERT statement throwing an error.

2. Check Constraint with Disable

The DISABLE option is nothing but it disables the declared constraint until unless it gets enabled. Constraint does not enforce the Check condition if the DISABLE keyword is being used with the constraint.

Code:

SQL>CREATE TABLE Mydept_1 (Deptno NUMBER (2) CONSTRAINT Dep_chk_1 CHECK (Deptno in (10,20,30,40)) DISABLE,Dname VARCHAR2 (16));

Output:

Disable

Explanation: The above CREATE statement created a new table Mydept_1 and the Deptno column consists of Check Constraint with DISABLE keyword. Now we’ll insert values and check the behavior.

Code:

SQL>INSERT INTO Mydept_1 VALUES (30,’SALES’);

Output:

keyword

Code:

SQL>INSERT INTO Mydept_1 VALUES (50,’SALES’);

Output:

Oracle CHECK Constraint6

Explanation: The above two INSERT statement inserted two different deptno values successfully. First deptno value 30 satisfies the Check condition but the second value 50 does not but still got inserted. WHY? Because the DISABLE keyword in Check Constraint keeps it in disable mode, so check condition does not enforce.

3. Check Constraint on Table Level (Out of Line) style

Code:

SQL >CREATE TABLE Mydept_2 (Deptno NUMBER (2),Dname VARCHAR2 (16),
CONSTRAINT Dep_chk_2 CHECK (Deptno in (10, 20, 30, 40)));

Output:

Oracle CHECK Constraint7

Explanation: The above Check statement creates a table Mydept_2 with CHECK CONSTRAINT but the constraint declared on Table level (out of line) style and the behavior is the same as a column-level style declaration.

4. Check Constraint Maintenance

Guidelines

  • Check Constraint can be ADDED, DROPPED, ENABLED, or DISABLED but cannot modify the physical structure of the table.
  • The CHECK Constraintname can be checked form the
  • USER_CONSTRAINTS
  • USER_CONS_COLUMNS data dictionary views

5. Viewing Check Constraint

Code:

SQL>SELECT constraint_name, constraint_type FROM USER_CONSTRAINTS WHERE                     TABLE_NAME=’MYDEPT_1′;

Output:

Oracle CHECK Constraint8

Explanation: Output showing Constraint_Name and Type. Here C denotes Check Constraint.

6. Adding Check Constraint

Code:

SQL>ALTER TABLE samp_pk ADD CONSTRAINT chk_sampid CHECK(samid in(8,9,4,3));

Output:

Oracle CHECK Constraint9

Explanation: Earlier SAMP_PK table had no Check Constraint. Now it has.

7. Dropping Check Constraint

Code:

SQL>ALTER TABLE Mydept_1 DROP CONSTRAINT DEP_CHK_1;

Output:

Oracle CHECK Constraint10

Explanation: In the above example Check Constraint (DEP_CHK_1) from Mydept_1 table got dropped. Now Emp_1 doesn’t have any Check Constraint (see image below).

Code:

SQL>SELECT constraint_name, constraint_type FROM USER_CONSTRAINTS WHERE                     TABLE_NAME='MYDEPT_1';

Output:

Oracle CHECK Constraint11

Check Constraint can be disabled or enabled as well.

Code:

SQL>ALTER TABLE Table_Name ENABLE / DISABLE CONSTRAINT <Constraint Name>];

Explanation: A single column can have multiple Check Constraints.

Conclusion

Oracle Check Constraint a CONSTRAINT that enables us to define a rule or condition to enforce it for limiting values in one column or more. To maintain the integrity and validate the condition before loading data, Check Constraint can be used.

Recommended Articles

This is a guide to Oracle Check Constraint. Here we discuss an introduction to Oracle Check Constraint, declaration style, syntax, examples with code, and output. You can also go through our other related articles to learn more –

  1. Oracle UNIQUE Constraint
  2. Oracle Constraints
  3. Oracle Clauses
  4. Oracle FOREIGN Key

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW