</>
Skip to content
MySQL lessons (38/48)

MySQL — Check

Basic CHECK Constraint

CREATE TABLE products (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    price DECIMAL(10,2) CHECK (price > 0),
    quantity INT CHECK (quantity >= 0)
);

Named CHECK

CREATE TABLE employees (
    id INT PRIMARY KEY,
    age INT,
    salary DECIMAL(10,2),
    CONSTRAINT chk_age CHECK (age >= 18 AND age <= 65),
    CONSTRAINT chk_salary CHECK (salary > 0)
);

CHECK with Multiple Columns

CREATE TABLE bookings (
    start_date DATE,
    end_date DATE,
    CHECK (end_date > start_date)
);

Adding CHECK After Creation

ALTER TABLE products ADD CONSTRAINT chk_price CHECK (price > 0);

Dropping CHECK

ALTER TABLE products DROP CHECK chk_price;

CHECK Examples

ConditionUse Case
CHECK (price > 0)Positive prices only
CHECK (email LIKE '%@%')Email format
CHECK (length(code) = 5)Fixed length
CHECK (status IN ('A','B','C'))Limited values

Mini Practice

Write SQL code that:

  1. Creates a table with CHECK constraints
  2. Adds a CHECK with a named constraint
  3. Creates a multi-column CHECK
  4. Tests the CHECK with valid and invalid data

Up Next

Continue with Default — providing automatic values.

Related Topics

Frequently Asked Questions about Check

What is Check in MySQL?

Check is a fundamental concept in MySQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Check?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Check.

Why is Check important in MySQL?

Check is essential for MySQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.