A CHECK constraint helps make sure the data in a table follows simple rules. It stops wrong or unexpected data from being saved.
CHECK constraint in SQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SQL
CREATE TABLE table_name ( column_name datatype CHECK (condition), ... ); -- Or add CHECK constraint after table creation: ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK (condition);
The condition inside CHECK must be something that returns true or false.
If the condition is false, the database will not allow the data to be saved.
Examples
SQL
CREATE TABLE Employees ( ID INT, Age INT CHECK (Age >= 18) );
SQL
CREATE TABLE Products ( ProductID INT, Price DECIMAL CHECK (Price >= 0) );
SQL
ALTER TABLE Orders ADD CONSTRAINT chk_status CHECK (Status IN ('Pending', 'Shipped', 'Cancelled'));
Sample Program
The first insert works because 85 is between 0 and 100. The second insert fails because 105 is outside the allowed range.
SQL
CREATE TABLE Students ( StudentID INT, Name VARCHAR(50), Grade INT CHECK (Grade BETWEEN 0 AND 100) ); INSERT INTO Students (StudentID, Name, Grade) VALUES (1, 'Alice', 85); INSERT INTO Students (StudentID, Name, Grade) VALUES (2, 'Bob', 105);
Important Notes
CHECK constraints help keep your data clean and reliable.
They are checked every time you insert or update data.
Not all database systems support complex CHECK constraints equally.
Summary
CHECK constraints enforce simple rules on data in a table.
They prevent invalid data from being saved.
You can add them when creating a table or later with ALTER TABLE.
Practice
1. What is the main purpose of a
CHECK constraint in SQL?easy
Solution
Step 1: Understand what a CHECK constraint does
A CHECK constraint sets a rule that data must follow when inserted or updated in a table.Step 2: Identify the purpose from the options
Only To enforce rules on column values to prevent invalid data describes enforcing rules on data to prevent invalid entries.Final Answer:
To enforce rules on column values to prevent invalid data -> Option DQuick Check:
CHECK constraint = enforce data rules [OK]
Hint: CHECK constraints prevent bad data from entering tables [OK]
Common Mistakes:
- Confusing CHECK with indexing or keys
- Thinking CHECK creates backups
- Assuming CHECK improves speed
2. Which of the following is the correct syntax to add a CHECK constraint on a column
age to allow only values greater than 18?easy
Solution
Step 1: Recall correct syntax for adding CHECK constraint
The correct syntax uses ADD CONSTRAINT with a name, then CHECK with parentheses around the condition.Step 2: Compare options to syntax
ALTER TABLE users ADD CONSTRAINT chk_age CHECK (age > 18); matches the syntax: ADD CONSTRAINT name CHECK (condition). Options A and C miss the constraint name or parentheses; D misses parentheses.Final Answer:
ALTER TABLE users ADD CONSTRAINT chk_age CHECK (age > 18); -> Option AQuick Check:
ADD CONSTRAINT name CHECK (condition) = ALTER TABLE users ADD CONSTRAINT chk_age CHECK (age > 18); [OK]
Hint: Use ADD CONSTRAINT name CHECK (condition) syntax [OK]
Common Mistakes:
- Omitting parentheses around condition
- Not naming the constraint
- Writing condition outside CHECK()
3. Given the table creation:
What happens if you run:
CREATE TABLE products ( id INT, price DECIMAL(5,2), CONSTRAINT chk_price CHECK (price >= 0) );
What happens if you run:
INSERT INTO products (id, price) VALUES (1, -10.00);
medium
Solution
Step 1: Understand the CHECK constraint on price
The constraint requires price to be greater than or equal to 0.Step 2: Analyze the insert statement
The insert tries to add price = -10.00, which violates the CHECK condition.Final Answer:
The insert fails due to CHECK constraint violation -> Option CQuick Check:
price < 0 violates CHECK = insert fails [OK]
Hint: CHECK rejects rows violating the condition [OK]
Common Mistakes:
- Assuming negative values are accepted
- Thinking CHECK auto-corrects values
- Confusing CHECK violation with syntax error
4. You try to create a table with this statement:
But get a syntax error. What is the likely fix?
CREATE TABLE employees ( id INT, salary INT CHECK salary > 0 );
But get a syntax error. What is the likely fix?
medium
Solution
Step 1: Identify syntax for inline CHECK constraints
Inline CHECK constraints require the condition to be inside parentheses.Step 2: Check the given statement
The statement misses parentheses aroundsalary > 0, causing syntax error.Final Answer:
Add parentheses around the CHECK condition: CHECK (salary > 0) -> Option BQuick Check:
Inline CHECK needs parentheses [OK]
Hint: Always use parentheses around CHECK conditions [OK]
Common Mistakes:
- Omitting parentheses in inline CHECK
- Changing data type unnecessarily
- Ignoring syntax error details
5. You want to ensure a
discount column in orders table is between 0 and 50 inclusive. Which CHECK constraint correctly enforces this?hard
Solution
Step 1: Understand the required range including boundaries
The discount must be at least 0 and at most 50, so boundaries are included.Step 2: Evaluate each option
B: CHECK (discount > 0 AND discount < 50) excludes 0 and 50 (strict inequalities). A: CHECK (discount >= 0 AND discount <= 50) includes boundaries with >= and <=. C: CHECK (discount BETWEEN 1 AND 50) excludes 0 (>=1 <=50). D: CHECK (discount != 0 OR discount != 50) is logically incorrect.Final Answer:
CHECK (discount >= 0 AND discount <= 50) -> Option AQuick Check:
Inclusive range needs >= and <= [OK]
Hint: Use >= and <= for inclusive CHECK ranges [OK]
Common Mistakes:
- Using > and < excludes boundary values
- Using incorrect bounds with BETWEEN
- Logical errors with OR instead of AND
