â The CHECK constraint in SQL is used to restrict the values that can be stored in a column or a combination of columns. It ensures that every inserted or updated value satisfies a specified logical condition, helping maintain data accuracy and business rules directly within the database.
đ What is the CHECK Constraint?
A CHECK constraint evaluates a Boolean expression whenever data is inserted or updated. If the condition evaluates to TRUE, the operation succeeds. If it evaluates to FALSE, the database rejects the operation.
Information
đ¯ Why Use CHECK?
The CHECK constraint helps prevent invalid or inconsistent data from entering the database.
- đ Enforce business rules.
- đ Prevent invalid data entry.
- đ Improve data integrity.
- đ Reduce application-level validation.
- đ Ensure consistent data across applications.
đ Basic Syntax
CHECK Constraint Syntax
CREATE TABLE table_name
(
column_name data_type
CHECK (condition)
);đĄ Create a Table with CHECK
Create a Students table where the student's age must be at least 18 years.
CHECK Constraint Example
CREATE TABLE Students
(
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT CHECK (Age >= 18)
);The database accepts only rows where Age is 18 or greater.
â Valid INSERT
This statement succeeds because the age satisfies the constraint.
Valid CHECK Example
INSERT INTO Students
(StudentID, Name, Age)
VALUES
(101, 'Alice', 20);â Invalid INSERT
This statement fails because the age does not satisfy the CHECK condition.
Invalid CHECK Example
INSERT INTO Students
(StudentID, Name, Age)
VALUES
(102, 'Bob', 16);Error
đ CHECK on Multiple Columns
A CHECK constraint can validate conditions involving multiple columns.
Table-Level CHECK Constraint
CREATE TABLE Employees
(
EmployeeID INT PRIMARY KEY,
Salary DECIMAL(10,2),
Bonus DECIMAL(10,2),
CHECK (Bonus <= Salary)
);This rule ensures that an employee's bonus never exceeds their salary.
â Add a CHECK Constraint to an Existing Table
You can add a CHECK constraint after a table has already been created.
Add CHECK Constraint
ALTER TABLE Students
ADD CONSTRAINT CHK_Students_Age
CHECK (Age >= 18);â Remove a CHECK Constraint
Removing a CHECK constraint uses database-specific syntax.
Drop CHECK Constraint (SQL Server Example)
ALTER TABLE Students
DROP CONSTRAINT CHK_Students_Age;Important
đ Common CHECK Constraint Examples
| Constraint | Purpose |
|---|---|
| CHECK (Age >= 18) | Allow only adult students. |
| CHECK (Salary > 0) | Ensure positive salaries. |
| CHECK (Quantity >= 0) | Prevent negative inventory. |
| CHECK (Marks BETWEEN 0 AND 100) | Limit marks to a valid range. |
| CHECK (Status IN ('Active', 'Inactive')) | Restrict allowed status values. |
đŧ Real-World Example
An online shopping system requires product prices to always be greater than zero.
Products Table
CREATE TABLE Products
(
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(10,2),
CHECK (Price > 0)
);This prevents products from being stored with zero or negative prices.
âī¸ CHECK vs NOT NULL vs UNIQUE
| Feature | CHECK | NOT NULL | UNIQUE |
|---|---|---|---|
| Validates Values | â Yes | â No | â No |
| Disallows NULL | â No | â Yes | Depends on the database. |
| Prevents Duplicates | â No | â No | â Yes |
| Main Purpose | Enforce business rules. | Require a value. | Ensure uniqueness. |
â ī¸ Common Mistakes
- â Writing conditions that conflict with valid business requirements.
- â Assuming every SQL database supports identical CHECK behavior.
- â Adding a CHECK constraint when existing data violates the rule.
- â Using overly complex expressions that reduce readability.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ CHECK validates data before it is stored.
- đ It enforces business rules at the database level.
- đ It can validate one column or multiple columns together.
- đ Invalid INSERT and UPDATE operations are rejected.
- đ Multiple CHECK constraints can exist in a table.
- đ Constraint syntax and capabilities may vary slightly across database systems.