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
- 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.
- 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).
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]);
- 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
SQL> CREATE TABLE Mydept(Deptno NUMBER(2) CONSTRAINT Dep_chk CHECK(Deptno in(10,20,30,40)),Dname VARCHAR(16));
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.
SQL>INSERT INTO Mydept VALUES (30,’SALES’);
SQL>INSERT INTO Mydept VALUES (50,’SALES’);
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.
SQL>CREATE TABLE Mydept_1 (Deptno NUMBER (2) CONSTRAINT Dep_chk_1 CHECK (Deptno in (10,20,30,40)) DISABLE,Dname VARCHAR2 (16));
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.
SQL>INSERT INTO Mydept_1 VALUES (30,’SALES’);
SQL>INSERT INTO Mydept_1 VALUES (50,’SALES’);
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
SQL >CREATE TABLE Mydept_2 (Deptno NUMBER (2),Dname VARCHAR2 (16),
CONSTRAINT Dep_chk_2 CHECK (Deptno in (10, 20, 30, 40)));
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
- 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_CONS_COLUMNS data dictionary views
5. Viewing Check Constraint
SQL>SELECT constraint_name, constraint_type FROM USER_CONSTRAINTS WHERE TABLE_NAME=’MYDEPT_1′;
Explanation: Output showing Constraint_Name and Type. Here C denotes Check Constraint.
6. Adding Check Constraint
SQL>ALTER TABLE samp_pk ADD CONSTRAINT chk_sampid CHECK(samid in(8,9,4,3));
Explanation: Earlier SAMP_PK table had no Check Constraint. Now it has.
7. Dropping Check Constraint
SQL>ALTER TABLE Mydept_1 DROP CONSTRAINT DEP_CHK_1;
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).
SQL>SELECT constraint_name, constraint_type FROM USER_CONSTRAINTS WHERE TABLE_NAME='MYDEPT_1';
Check Constraint can be disabled or enabled as well.
SQL>ALTER TABLE Table_Name ENABLE / DISABLE CONSTRAINT <Constraint Name>];
Explanation: A single column can have multiple Check Constraints.
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.
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 –