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
Recall & Review
beginner
What is a CHECK constraint in SQL?
A CHECK constraint is a rule that limits the values allowed in a column. It ensures data meets specific conditions before being saved.
Click to reveal answer
beginner
How does a CHECK constraint help maintain data quality?
It prevents invalid data by allowing only values that satisfy the condition defined in the CHECK constraint.
Click to reveal answer
beginner
Write a simple CHECK constraint to ensure age is 18 or older.
CHECK (age >= 18)
Click to reveal answer
intermediate
Can a CHECK constraint use multiple conditions? Give an example.
Yes, using AND or OR. Example: CHECK (age >= 18 AND age <= 65) ensures age is between 18 and 65.
Click to reveal answer
beginner
What happens if you try to insert data that violates a CHECK constraint?
The database rejects the insert or update and shows an error, preventing invalid data from being saved.
Click to reveal answer
What does a CHECK constraint do in a database?
ACreates a backup of the database
BLimits values in a column based on a condition
CDeletes duplicate rows automatically
DEncrypts data in a column
✗ Incorrect
A CHECK constraint limits the values allowed in a column by enforcing a condition.
Which of the following is a valid CHECK constraint for a salary column to be positive?
ACHECK (salary > 0)
BCHECK salary > 0
CCHECK salary = positive
DCHECK (salary IS NOT NULL)
✗ Incorrect
The correct syntax uses parentheses and a condition: CHECK (salary > 0).
Can a CHECK constraint use logical operators like AND or OR?
AOnly AND is allowed, not OR
BNo, only one condition is allowed
COnly OR is allowed, not AND
DYes, to combine multiple conditions
✗ Incorrect
CHECK constraints can combine multiple conditions using AND or OR.
What happens if data violates a CHECK constraint during insertion?
AThe database automatically fixes the data
BThe data is inserted anyway
CThe insertion fails with an error
DThe data is inserted but marked invalid
✗ Incorrect
The database rejects the insert and shows an error to maintain data integrity.
Which SQL keyword is used to add a CHECK constraint when creating a table?
ACHECK
BLIMIT
CCONDITION
DRESTRICT
✗ Incorrect
The CHECK keyword defines the condition for the constraint.
Explain what a CHECK constraint is and how it helps keep data correct.
Think about rules that data must follow before saving.
You got /3 concepts.
Write an example of a CHECK constraint that ensures a product price is greater than zero and less than 1000.
Use AND to combine two conditions inside CHECK.
You got /4 concepts.
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]