Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using CHECK Constraint to Validate Data in SQL
📖 Scenario: You are creating a simple database table to store information about employees in a company. You want to make sure that the age of each employee is a reasonable number, between 18 and 65 years old.
🎯 Goal: Build a SQL table called Employees with columns for EmployeeID, Name, and Age. Use a CHECK constraint to ensure that the Age is always between 18 and 65.
📋 What You'll Learn
Create a table named Employees with columns EmployeeID (integer), Name (text), and Age (integer).
Add a CHECK constraint on the Age column to allow only values between 18 and 65 inclusive.
Use standard SQL syntax compatible with common databases like PostgreSQL or MySQL.
💡 Why This Matters
🌍 Real World
Companies use CHECK constraints to make sure data like ages, salaries, or IDs stay within valid ranges, preventing mistakes.
💼 Career
Database administrators and developers use CHECK constraints to enforce business rules directly in the database, improving data quality and reliability.
Progress0 / 4 steps
1
Create the Employees table with basic columns
Write a SQL statement to create a table called Employees with three columns: EmployeeID as an integer, Name as text, and Age as an integer.
SQL
Hint
Use CREATE TABLE followed by the table name and define each column with its data type inside parentheses.
2
Add a CHECK constraint for Age limits
Modify the Employees table creation statement to add a CHECK constraint on the Age column that allows only values between 18 and 65 inclusive.
SQL
Hint
Use CHECK (Age >= 18 AND Age <= 65) right after the Age INT column definition.
3
Insert valid employee data
Write a SQL INSERT statement to add an employee with EmployeeID 1, Name 'Alice', and Age 30 into the Employees table.
SQL
Hint
Use INSERT INTO Employees (EmployeeID, Name, Age) VALUES (1, 'Alice', 30); to add the data.
4
Try inserting invalid employee data
Write a SQL INSERT statement to add an employee with EmployeeID 2, Name 'Bob', and Age 17 into the Employees table. This should violate the CHECK constraint.
SQL
Hint
Use INSERT INTO Employees (EmployeeID, Name, Age) VALUES (2, 'Bob', 17); to test the constraint.
Practice
(1/5)
1. What is the main purpose of a CHECK constraint in SQL?
easy
A. To automatically generate unique IDs
B. To create a backup of the table data
C. To speed up query performance
D. To enforce rules on column values to prevent invalid data
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 D
Quick 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
A. ALTER TABLE users ADD CONSTRAINT chk_age CHECK (age > 18);
B. ALTER TABLE users CHECK (age > 18);
C. ALTER TABLE users ADD CHECK age > 18;
D. ALTER TABLE users ADD CONSTRAINT chk_age CHECK age > 18;
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 A
Quick 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]