Categories: SQL Tutorials

What is SQL Check Constraint ? | SQL Check constraint examples

SQL Check Constraint :

In my previous article i have given idea of different types of constraints in SQL as well as SQL Not null Constraint with its examples.  I have explained about SQL Unique Constraint with examples also. This article gives you the idea of new SQL constraint which is used to limit the value range at column level. SQL Check Constraint is mostly used in so many real life scenarios in industry to restrict the value for the specific range.

The CHECK constraint is used to limit the value range that can be placed in a column.

How to add SQL Check Constraint ?

If you define a CHECK constraint on a single column it allows only certain values for this 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.

Real Life Scenario:

CREATE TABLE Student

(

Class Number (5),

RollNo Number (10),

Subject Varchar2 (15),

Fees Number (10, 2) CHECK (Fees>500)

);

Example:

 INSERT INTO Student (Class, RollNo, Subject, Fees)

Values (5, 105, ‘Eng’, 550);

INSERT INTO Student (Class, RollNo, Subject, Fees)

Values (5, 105, ‘Eng’, 50);

ERROR at line 1:

ORA-02290: check constraint (Amit.SYS_C004958) violated

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:

CREATE TABLE Student

(

Class Number (5),

RollNo Number (10),

Subject Varchar2 (15),

Fees Number (10),

CONSTRAINT C_Student_Fees_Subject CHECK (Fees>500 AND Subject=’English’)

);

Example:

INSERT INTO Student (Class, RollNo, Subject, Fees)

Values (5, 10, ‘English’, 650);

INSERT INTO Student4A (Class, RollNo, Subject, Fees)

Values (5, 10, ‘Maths’, 350);

ERROR at line 1:

ORA-02290: check constraint (Amit.C_STUDENT_FEES_SUBJECT) violated

Note: You can use AND/OR Operators in CHECK Constraint.

Example :

CREATE TABLE Student4B

(

Class Number (5),

RollNo Number (10),

Subject Varchar2 (15),

Fees Number (10),

CONSTRAINT C_Student4B_Fees_Subject CHECK (Fees>500 OR Subject=’English’)

);

Example:

INSERT INTO Student4B (Class, RollNo, Subject, Fees)

Values (5, 10, ‘English’, 50);

INSERT INTO Student4B (Class, RollNo, Subject, Fees)

Values (5, 10, ‘Maths’, 650);

INSERT INTO Student4B (Class, RollNo, Subject, Fees)

Values (5, 10, ‘Science’, 400);

ERROR at line 1:

ORA-02290: check constraint (Amit.C_STUDENT4B_FEES_SUBJECT) violated

To DROP a CHECK SQL Constraints:

To Drop a CHECK Constraint, use the following SQL.

Syntax:

 ALTER TABLE <Table_Name>

DROP CONSTRAINT <Constraint_Name>;

Example:

ALTER TABLE Student

DROP Constraint SYS_C005434;

 

ALTER TABLE Student

DROP CONSTRAINT C_Student_Fees_Subject;

SQL CHECK Constraint on ALTER TABLE:

Syntax:

ALTER TABLE <Table_Name>

ADD <Constraint_Type> (Column_Name);

ALTER TABLE Student

ADD CHECK (RollNo>5);

OR

 

ALTER TABLE Student MODIFY Class Number (10) CHECK (Class>5);

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:

Syntax:

ALTER TABLE <Table_Name>

ADD CONSTRAINT <Constraint_Name> CONSTRAINT_TYPE (Column_Name);

ALTER TABLE Student

ADD CONSTRAINT C_Student_Subject_Fees CHECK (Subject=’Hindi’ AND Fees>500);

ALTER TABLE Student

ADD CONSTRAINT C_Student_Subject_Fees CHECK (Subject=’Hindi’ OR Fees>500);

Hope you got the idea about the SQL Check Constraint with different real life examples. This article will help you practically understanding the SQL Check Constraint with its real examples. User will be able to use the check constraint in real life scenario. Please comment in comment section if you like this article on SQL check constraint.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Recent Posts

The Essential Guide to Cryptocurrency Exchange Platform Development for Beginners and Beyond

Introduction Cryptocurrencies took the world by storm, setting up a new financial system and breaking…

5 months ago

Top 20 System Administrator Interview Questions and answers

In my previous article I have given Top 20 technical support interview questions with its…

8 months ago

Desktop Support Scenario Based Interview Questions

In my previous articles I have given 15 most asked desktop support interview questions with…

8 months ago

How Do Business Analysts Use SQL for Their Needs?

A business analyst is someone who is versed in processes of data analysis used for…

8 months ago

Top 15 Control-M Interview Questions with Answers

In my previous article I have already given top questions and answers for Desktop support…

8 months ago

Top 20 SQL Interview Questions for Production Support

In my previous article I have given unix production support interview questions and answers. In…

8 months ago