Lesson 38 of 60 – CHECK Constraint
63%

CHECK Constraint in SQL

The CHECK constraint is used to restrict the values that can be stored in a column. It allows you to define a condition that data must satisfy before it is inserted or updated.

Note: A CHECK constraint helps maintain valid data by allowing only values that satisfy the specified condition.

1. What is CHECK Constraint?

The CHECK constraint is used to ensure that values in a column satisfy a specified condition.

CREATE TABLE students (
    age INT CHECK (age >= 18)
);

Here, the age must be 18 or greater.

2. Why Use CHECK?

CHECK is useful when you want to prevent invalid data from being stored.

For example:

  • Age should be at least 18.
  • Marks should be between 0 and 100.
  • Salary should be greater than 0.
  • Status should contain an allowed value.

3. Basic CHECK Syntax

The basic syntax is:

column_name data_type CHECK (condition)

Example:

age INT CHECK (age >= 18)

4. CHECK During CREATE TABLE

You can define a CHECK constraint while creating a table.

CREATE TABLE students (
    student_id INT,
    age INT CHECK (age >= 18)
);

The database checks the age condition when data is inserted or updated.

5. Valid CHECK Value

If the value satisfies the CHECK condition, the record can be inserted.

CREATE TABLE students (
    age INT CHECK (age >= 18)
);

INSERT INTO students (age)
VALUES (25);

25 satisfies the condition age >= 18.

6. Invalid CHECK Value

If the value does not satisfy the condition, the database rejects the operation.

INSERT INTO students (age)
VALUES (15);

This violates CHECK (age >= 18).

7. CHECK with Numeric Values

CHECK is commonly used with numeric columns.

CREATE TABLE products (
    price DECIMAL(10,2) CHECK (price > 0)
);

The price must be greater than zero.

8. CHECK with Marks

You can use CHECK to restrict marks between 0 and 100.

CREATE TABLE results (
    student_name VARCHAR(100),
    marks INT CHECK (marks >= 0 AND marks <= 100)
);

Values such as 50 are valid, while 120 are invalid.

9. CHECK with Salary

Salary can be restricted to positive values.

CREATE TABLE employees (
    employee_id INT,
    salary DECIMAL(10,2) CHECK (salary > 0)
);

A salary of 0 or a negative salary would violate the condition.

10. CHECK with Age Range

You can restrict age to a specific range.

CREATE TABLE students (
    age INT CHECK (age >= 5 AND age <= 60)
);

The age must be between 5 and 60.

11. CHECK with Text Values

CHECK can also be used to restrict text values.

CREATE TABLE students (
    status VARCHAR(20)
    CHECK (status IN ('Active', 'Inactive'))
);

Only the specified status values are allowed by this condition.

12. CHECK with Gender Values

A CHECK condition can restrict a column to specific values.

CREATE TABLE students (
    gender VARCHAR(10)
    CHECK (gender IN ('Male', 'Female', 'Other'))
);

Other values violate the condition.

13. CHECK with Multiple Conditions

Multiple conditions can be combined using logical operators.

CREATE TABLE students (
    age INT CHECK (age >= 18 AND age <= 60)
);

Both conditions must be satisfied.

14. CHECK with OR

The OR operator can be used when more than one condition is acceptable.

CREATE TABLE employees (
    department VARCHAR(30)
    CHECK (
        department = 'IT'
        OR department = 'HR'
    )
);

The department must satisfy at least one of the conditions.

15. Named CHECK Constraint

You can give a CHECK constraint a specific name.

CREATE TABLE students (
    age INT,
    CONSTRAINT chk_student_age CHECK (age >= 18)
);

The constraint is named chk_student_age.

16. Multiple CHECK Constraints

A table can have multiple CHECK constraints.

CREATE TABLE students (
    age INT CHECK (age >= 18),
    marks INT CHECK (marks >= 0 AND marks <= 100)
);

Each column has its own validation rule.

17. CHECK During INSERT

The CHECK condition is evaluated when a row is inserted.

CREATE TABLE products (
    price DECIMAL(10,2)
    CHECK (price > 0)
);

INSERT INTO products (price)
VALUES (500);

The value 500 satisfies the condition.

18. CHECK During UPDATE

CHECK is also important when existing records are updated.

UPDATE products
SET price = -100
WHERE product_id = 1;

If the table has CHECK (price > 0), this update violates the constraint.

19. CHECK with NOT NULL

CHECK and NOT NULL can be used together.

CREATE TABLE students (
    age INT NOT NULL CHECK (age >= 18)
);

NOT NULL requires a value, while CHECK validates that value.

20. CHECK with DEFAULT

CHECK can also be combined with DEFAULT.

CREATE TABLE products (
    quantity INT DEFAULT 0
    CHECK (quantity >= 0)
);

The default quantity is 0, and negative quantities are not allowed by the CHECK condition.

21. CHECK and PRIMARY KEY

CHECK and PRIMARY KEY have different purposes.

  • PRIMARY KEY uniquely identifies a record.
  • CHECK validates whether a value satisfies a condition.
CREATE TABLE students (
    student_id INT PRIMARY KEY,
    age INT CHECK (age >= 18)
);

22. CHECK and UNIQUE

UNIQUE and CHECK solve different problems.

  • UNIQUE prevents duplicate values.
  • CHECK restricts values according to a condition.
CREATE TABLE students (
    email VARCHAR(150) UNIQUE,
    age INT CHECK (age >= 18)
);

23. CHECK with ALTER TABLE

You can add a CHECK constraint to an existing table.

ALTER TABLE students
ADD CONSTRAINT chk_age
CHECK (age >= 18);

The existing data should be compatible with the new constraint.

24. Dropping a CHECK Constraint

A named CHECK constraint can be removed using ALTER TABLE in systems that support dropping named constraints.

ALTER TABLE students
DROP CHECK chk_age;

The exact syntax can vary between database systems.

25. CHECK with BETWEEN

CHECK can use BETWEEN to define a range.

CREATE TABLE students (
    marks INT CHECK (marks BETWEEN 0 AND 100)
);

Marks must be within the specified range.

26. CHECK with IN

CHECK can use IN to allow a predefined set of values.

CREATE TABLE employees (
    status VARCHAR(20)
    CHECK (status IN ('Active', 'Inactive', 'On Leave'))
);

The status must match one of the allowed values.

27. Common CHECK Mistake

A common mistake is defining a condition that does not match the actual business requirement.

CHECK (marks >= 50)

This would reject all marks below 50. If the requirement is to allow marks from 0 to 100, the condition should be designed accordingly.

28. CHECK and Data Integrity

CHECK constraints help maintain data integrity by preventing values that violate defined rules.

For example, a fee should not normally be negative.

fee DECIMAL(10,2) CHECK (fee >= 0)

29. Practical Student Example

Let's create a student table with multiple CHECK constraints.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    age INT CHECK (age >= 5 AND age <= 60),
    marks INT CHECK (marks >= 0 AND marks <= 100),
    status VARCHAR(20)
    CHECK (status IN ('Active', 'Inactive'))
);

This table validates age, marks, and status.

30. Complete CHECK Example

Here is a complete example using several constraints together.

CREATE TABLE students (
    student_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    age INT NOT NULL CHECK (age >= 18),
    marks INT CHECK (marks >= 0 AND marks <= 100),
    status VARCHAR(20) NOT NULL DEFAULT 'Active'
        CHECK (status IN ('Active', 'Inactive'))
);

INSERT INTO students
(name, age, marks, status)
VALUES
('Rahul', 22, 85, 'Active'),
('Priya', 20, 92, 'Active');

The table uses PRIMARY KEY, NOT NULL, DEFAULT, and CHECK constraints to help maintain valid student data.

📌 Key Points

  • CHECK restricts values according to a specified condition.
  • It helps prevent invalid data.
  • CHECK can be used with numeric and text values.
  • CHECK can validate ranges using BETWEEN.
  • CHECK can restrict values using IN.
  • Multiple CHECK constraints can be used in one table.
  • CHECK is evaluated during INSERT and UPDATE operations.
  • CHECK can be combined with NOT NULL and DEFAULT.
  • Named CHECK constraints can be added or removed using ALTER TABLE.
  • CHECK and UNIQUE have different purposes.
  • CHECK helps maintain data integrity.

🧠 Quick Quiz

Question: What is the main purpose of the CHECK constraint?