EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle Check Constraint
Secondary Sidebar
Oracle Tutorial
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Oracle Check Constraint

By Priya PedamkarPriya Pedamkar

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.

Declaration Style of Oracle Check Constraint

It can be declared in two ways. They are

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,112 ratings)
  • 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
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more