MySQL CHECK Constraint

Introduction

The CHECK constraint ensures that values inserted into a column meet a specific condition.

It is used to enforce data validation rules in a table.


Example

CREATE TABLE employees (
id INT,
age INT CHECK (age >= 18)
);

This ensures that employees must be at least 18 years old.


Multiple Conditions

CHECK constraints can include more complex conditions.

Example:

CHECK (salary > 0)

Benefits

  • Prevents invalid data entry

  • Enforces business rules

  • Improves data quality


Key Points

  • CHECK validates column values

  • Conditions must be satisfied before insertion

  • Helps maintain data accuracy