---
title: "Why are use the SQL CHECK Constraint in the database at table creation?"  
description: "Why are use the SQL CHECK Constraint in the database at table creation?"  
author: "Ravi Vishwakarma"  
published: 2021-09-22  
updated: 2021-09-22  
canonical: https://www.mindstick.com/forum/156741/why-are-use-the-sql-check-constraint-in-the-database-at-table-creation  
category: "database"  
tags: ["database", "mysql", "sql server", "sql"]  
reading_time: 2 minutes  

---

# Why are use the SQL CHECK Constraint in the database at table creation?

Why are use the [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) [CHECK](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) [Constraint in the database](https://www.mindstick.com/forum/156739/what-is-the-use-of-sql-foreign-key-constraint-in-the-database) at [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) [creation](https://yourviews.mindstick.com/view/85506/what-was-the-causes-of-creation-of-pakistan)?

## Replies

### Reply by Ashutosh Kumar Verma

**SQL [Check Constraint](https://www.mindstick.com/interview/1931/what-is-the-difference-between-a-check-constraint-and-a-rule):**

CHECK constraint is used to give a limit for the values to be passed on the column.

Data is inserted on the column according to the condition on which the CHECK constraint is given. A check constraint is used in SQL server allow you to specify condition on each row in a table.

CHECK constraints are column-level (single column) and table-level (multiple columns).

Check constraint is create two times, first when [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) table is created and other is alter table in exists table in database

## 1- SQL Check Constraint on table create:

The following SQL creates a Check constraint on the 'Age' column when the 'Student' table is created. The Check constraint ensures that the age of a Student must be 18, or older:

```
create table Student(
stuID int not null primary key,
stuName varchar(255),
stuCourse varchar(255),
stuAge int CHECK (stuAge>=18) );
```

Check constraint on multiple columns in a table

```
create table StudentRecord(
stuID int not null primary key,
stuName varchar(255),
stuCourse varchar(255),
stuAge int ,
stuMarks int ,
CONSTRAINT chk_StudentRecord CHECK (stuAge>=18 AND stuMarks>=40) );
```

## \

## 2- SQL Check Constraint on created table:

The following SQL creates a Check Constraint on student Age in “Student” table which is created in database. Here the Check Constraint is used with SQL ALTER command. Ex- for single column in table,

```
ALTER TABLE Student
ADD CHECK (stuAge>=18);
```

## For multiple columns of a table,

```
ALTER TABLE StudentRecord
ADD CONSTRAINT chk_Students CHECK (stuAge>=18 AND stuMarks>=40);
```

In the above Student table the records entered in which stuAge must be 18 or above and stuID is more than 3.

## Drop Check Constraint:

To Drop a created Check Constraint in table, use the following SQL statement,

```
ALTER TABLE Student
DROP CONSTRAINT chk_Student;
```

Other one is-

```
alter table StudentRecord
drop constraint chk_StudentRecord;
```

\


---

Original Source: https://www.mindstick.com/forum/156741/why-are-use-the-sql-check-constraint-in-the-database-at-table-creation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
