A CHECK constraint specifies a condition that column values must satisfy before the database accepts the row.
CHECK constraints help protect data quality by preventing invalid values from being stored in the database.
An employee salary should not be negative. A CHECK constraint can enforce Salary >= 0.
CREATE TABLE Employees (
EmployeeID INT,
EmployeeName VARCHAR(100),
Salary DECIMAL(12,2),
CHECK (Salary >= 0)
);
CHECK constraints are useful for validating salaries, quantities, statuses, percentages, dates, and other business rules.
- CHECK enforces a condition on data.
- Invalid values can be rejected.
- It helps maintain data quality.
- CHECK can represent business rules.
- The exact behavior can vary by database system.
What is a CHECK constraint?
A CHECK constraint ensures that inserted or updated values satisfy a specified condition.
Why use CHECK constraints?
They help prevent invalid data from being stored and enforce important business rules.