×

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used on the table as well as on the column. So we can say, Check constraint can be created on the Column level and Table Level.

Check Constraint applied on the column, and it will allow certain values for this column while inserting into the column. If you define a CHECK constraint on a table, it can limit the values in certain columns based on values in other columns in the row. We can apply multiple Check Constraints in a single table.

Let's see certain examples to understand the Check Constraint concept more clearly.

Column Level Check Constraint

The check constraint is defined just after the column name, known to be Column Level Check Constraint. Syntax of Column Level Check Constraint as follows,

CREATE TABLE TABLE_NAME(COLUMN_NAME1 DATATYPE(SIZE), COLUMN_NAME2 DATATYPE(SIZE) CHECK(COLUMN_NAME CONDITION), COLUMN_NAME3 DATATYPE(SIZE));

For example, we will create a table and define a column-level check constraint on one of the following columns in a table:

CREATE TABLE EMPLOYEES (EMPLOYEEID INT PRIMARY KEY, EMPLOYEE_NAME VARCHAR(50) NOT NULL, SALARY INT CHECK(SALARY > 40000), CITY VARCHAR(20) NOT NULL, DEPARTMENT VARCHAR(30) NOT NULL);

In the above query, we have created a table named Employees and defined certain columns. In the table Employee, we have specified check constraints on the salary column. The constraint itself says that the salary column will accept those data only whose employee salary is greater than 40000; if the salary is less than 40000, constraint violation errors will occur.

To cross-check that the CHECK constraint is defined on the Salary column, we will use the below query:

SHOW CREATE TABLE EMPLOYEES;
Check Constraint in SQL

We will insert a record in the Employee table where an employee's salary is less than 40000.

INSERT INTO EMPLOYEES VALUES (1171101, 'Parag Chordia', 38000, 'Pune', 'Java');
Check Constraint in SQL

When we inserted an employee record whose salary is less than 40000, the constraint failed error message is displayed; why? Because we applied Check constraint on a salary which will allow only those records whose employee salary are greater than 40000.

We will insert a record in the Employee table where an employee's salary is greater than 40000.

INSERT INTO EMPLOYEES VALUES (1171101, 'Parag Chordia', 45000, 'Pune', 'Java');

To Cross check whether data is inserted in a table, we will use the below query:

SELECT * FROM EMPLOYEES;
Check Constraint in SQL

An employee record is successfully inserted into the table as we entered Employee Salary greater than 40000.

Example 2: We will create a table and define a column-level check constraint on more than one column in a table.

CREATE TABLE MANAGER(MANAGERID INT PRIMARY KEY, NAME VARCHAR(40) NOT NULL, SALARY INT CHECK(SALARY>=60000), DEPARTMENT VARCHAR(20) NOT NULL CHECK(DEPARTMENT IN('Oracle', 'FMW', 'Testing', 'Java' )));

We have created a table name Manager in the above query and defined certain columns. We have specified check constraints on the salary column in a table Manager. The constraint itself says that the salary column will accept those data only whose manager salary is greater than 60000; if the salary is less than 60000, constraint violation errors will occur, and the manager's Department is Oracle, FMW, Testing, and Java.

To cross-check that the CHECK constraint is defined on the Salary column and Department Column, we will use the below query:

SHOW CREATE TABLE MANAGER;
Check Constraint in SQL

We will insert a record in the Manager table where the salary of a manager is less than 60000 and the Department is Java.

INSERT INTO MANAGER VALUES(1, 'Bhavesh Bardiya', 59500, 'Java');
Check Constraint in SQL

When we inserted a manager record whose salary is less than 60000, the constraint failed error message is displayed; why? Because we applied Check constraint on a salary which will allow only those records whose manager salary are greater than 60000. But no constraint error occurred on the Department column because we inserted those values which column allowed to be inserted,

We will insert a record in the Manager table where the salary of a manager is greater than 60000, and the Department is Java.

INSERT INTO MANAGER VALUES(1, 'Bhavesh Bardiya', 62000, 'Java');

To Cross check whether data is inserted in a table, we will use the below query:

SELECT * FROM MANAGER;
Check Constraint in SQL

The manager record is successfully inserted into the table as we entered Manager Salary greater than 60000 and Department name Java.

Table Level Check Constraint

The check constraint is defined at the end of the table, known as the Table Level Check Constraint. Syntax of Table Level Check Constraint as follows,

CREATE TABLE TABLE_NAME(COLUMN_NAME1 DATATYPE(SIZE), COLUMN_NAME2 DATATYPE(SIZE), COLUMN_NAME3 DATATYPE(SIZE), CONSTRAINT CONSTAINT_NAME CHECK(COLUMN_NAME CONDITION));

Example 1: We will create a table and define a Table level check constraint on one of the following columns in a table.

CREATE TABLE EMPLOYEES (EMPLOYEEID INT PRIMARY KEY, EMPLOYEE_NAME VARCHAR(50) NOT NULL, SALARY INT, CITY VARCHAR(20) NOT NULL, DEPARTMENT VARCHAR(30) NOT NULL, CONSTRAINT salary_constraint CHECK(SALARY > 40000));

In the above query, we have created a table named Employees and defined certain columns. In the table Employee, we have specified check constraints on the salary column. The constraint itself says that the salary column will accept those data only whose employee salary is greater than 40000; if the salary is less than 40000 constraints, violation errors will occur.

To cross-check that the CHECK constraint is defined on the Salary column, we will use the below query:

SHOW CREATE TABLE EMPLOYEES;
Check Constraint in SQL

We will insert a record in the Employee table where an employee's salary is less than 40000.

INSERT INTO EMPLOYEES VALUES (1171101, 'Naman Sharma', 38000, 'Pune', 'Java');
Check Constraint in SQL

When we inserted an employee record whose salary is less than 40000, the constraint failed error message is displayed; why? Because we applied Check constraint on a salary which will allow only those records whose employee salary are greater than 40000.

We will insert a record in the Employee table where an employee's salary is greater than 40000.

INSERT INTO EMPLOYEES VALUES (1171101, 'Naman Sharma', 45000, 'Pune', 'Java');

To Cross check whether data is inserted in a table, we will use the below query:

SELECT * FROM EMPLOYEES;
Check Constraint in SQL

An employee record is successfully inserted into the table as we entered Employee Salary greater than 40000.

Example 2: We will create a table and define a column-level check constraint on more than one column in a table.

CREATE TABLE MANAGER (MANAGERID INT PRIMARY KEY, NAME VARCHAR(40) NOT NULL, SALARY INT, DEPARTMENT VARCHAR(40) NOT NULL, CONSTRAINT SALARY_CONST CHECK(SALARY>60000), CONSTRAINT DEPT_CONST CHECK(DEPARTMENT IN(‘'Oracle', 'FMW', ‘Java’, 'Testing')));

We have created a table name Manager in the above query and defined certain columns. We have specified check constraints on the salary column in a table Manager. The constraint itself says that the salary column will accept those data only whose manager salary is greater than 60000; if the salary is less than 60000, constraint violation errors will occur, and the manager's Department is Oracle, FMW, Testing, and Java.

To cross-check that the CHECK constraint is defined on the Salary column and Department Column, we will use the below query:

SHOW CREATE TABLE MANAGER;
Check Constraint in SQL

We will insert a record in the Manager table where the salary of a manager is less than 60000 and the Department is Java.

INSERT INTO MANAGER VALUES(1, 'Surili Jain', 59500, 'Java');
Check Constraint in SQL

When we inserted a manager record whose salary is less than 60000, the constraint failed error message is displayed; why? Because we applied Check constraint on a salary which will allow only those records whose manager salary are greater than 60000. But no constraint error occurred on the Department column because we inserted those values which column allowed to be inserted,

We will insert a record in the Manager table where the salary of a manager is greater than 60000, and the Department is Java.

INSERT INTO MANAGER VALUES(1, 'Surili Jain', 62500, 'Java');

To Cross check whether data is inserted in a table, we will use the below query:

SELECT * FROM MANAGER;
Check Constraint in SQL

The manager record is successfully inserted into the table as we entered Manager Salary greater than 60000 and Department name Java.

Check Constraint Using Alter

We created a table and forgot to add CHECK CONSTRAINT while creating a table, and then we need to add CHECK CONSTRAINT in a table. In such cases, we will use ALTER command to apply the CHECK CONSTRAINT on the existing table.

Syntax of Check Constraint using Alter as follows,

ALTER TABLE TABLE_NAME ADD CONSTRAINT CONSTRAINT_NAME CHECK (COLUMN_NAME CONDITION);

Example 1: Suppose we created an Employees table without adding Check Constraints. Now we want to add Check Constraint on one of the columns. Then we will use the below query:

ALTER TABLE EMPLOYEES ADD CONSTRAINT Sal_Constraint CHECK (SALARY > 35000);  

To cross-check that the CHECK constraint is defined on the Salary column, we will use the below query:

SHOW CREATE TABLE EMPLOYEES;
Check Constraint in SQL

We will insert a record in the Employee table where an employee's salary is less than 35000.

INSERT INTO EMPLOYEES VALUES (1001, 'Abhinav Patil', 30000, 'Mumbai', 'Testing');
Check Constraint in SQL

When we inserted an employee record whose salary is less than 35000, the constraint failed error message is displayed; why? Because we applied Check constraint on a salary which will allow only those records whose employee salary are greater than 35000.

Example 2: Suppose we created an Employees table without adding Check Constraints. Now we want to add Check Constraint on one of the columns. Then we will use the below query:

ALTER TABLE EMPLOYEES ADD CONSTRAINT City_Constraint CHECK (CITY IN ('Mumbai', 'Pune', 'Bangalore', 'Chennai'));

To cross-check that the CHECK constraint is defined on the City column, we will use the below query:

SHOW CREATE TABLE EMPLOYEES;
Check Constraint in SQL

We will insert a record in the Employee table where the City of an employee is Jaipur.

SHOW CREATE TABLE EMPLOYEES;
Check Constraint in SQL

When we inserted an employee record whose city name is Jaipur, the constraint failed error message is displayed; why? Because we applied Check constraint on City which will allow only those records where city name will be 'Mumbai', 'Pune', 'Bangalore', or 'Chennai’.

Drop Check Constraint

Suppose we have defined a CHECK CONSTRAINT on the table columns. Later we want to delete that CONSTRAINT from the column. Then we will use ALTER command to drop the CHECK CONSTRAINT.

Syntax of Drop Check Constraint as follows,

ALTER TABLE TABLE_NAME DROP CONSTRAINT CONSTRAINT_NAME;

Example 1: Suppose we have defined Check Constraint on one of the columns of the Employees table. Later, we decided to drop that constraint.

We will first check created constraint, for this use following query:

SHOW CREATE TABLE EMPLOYEES;
Check Constraint in SQL

We will write the below query to drop constraint named 'City_Constraint’.

ALTER TABLE EMPLOYEES DROP CONSTRAINT City_Constraint;

We will again use the SHOW CREATE TABLE query to check that constraint is dropped successfully.

SHOW CREATE TABLE EMPLOYEES;
Check Constraint in SQL

Example 2: Suppose we have defined Check Constraint on one of the columns of the Manager Table. Later, we decided to drop that constraint.

We will first check created constraint, for this use following query:

SHOW CREATE TABLE MANAGER;
Check Constraint in SQL

We will write the below query to drop constraint named 'SALARY_CONST'.

ALTER TABLE MANAGER DROP CONSTRAINT SALARY_CONST;

We will again use the SHOW CREATE TABLE query to check that constraint is dropped successfully.

SHOW CREATE TABLE MANAGER;
Check Constraint in SQL

Related Topics

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.