Bird
Raised Fist0
SQLquery~10 mins

CHECK constraint in SQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - CHECK constraint
Start Table Creation
Define Columns
Add CHECK Constraint
Insert/Update Data
Check Condition on Data
Allow Data
End
When creating or modifying a table, a CHECK constraint sets a rule. When data is added or changed, the database checks if the data meets the rule. If yes, it accepts the data; if no, it rejects it.
Execution Sample
SQL
CREATE TABLE Employees (
  ID INT,
  Age INT CHECK (Age >= 18)
);

INSERT INTO Employees VALUES (1, 20);
INSERT INTO Employees VALUES (2, 16);
This code creates a table with a rule that Age must be 18 or older. It tries to add two employees, one with Age 20 (allowed) and one with Age 16 (rejected).
Execution Table
StepActionData CheckedConditionResultNotes
1Create table EmployeesN/AN/ASuccessTable created with CHECK constraint Age >= 18
2Insert (1, 20)Age=2020 >= 18AllowedData meets CHECK constraint
3Insert (2, 16)Age=1616 >= 18RejectedData violates CHECK constraint
💡 Insertion stops when data violates CHECK constraint (Age < 18)
Variable Tracker
VariableStartAfter Step 2After Step 3
Employees Table RowsEmpty(1, 20)(1, 20) only; (2, 16) rejected
Key Moments - 2 Insights
Why was the second insert rejected even though the table exists?
Because the CHECK constraint requires Age to be 18 or more. The second insert has Age=16, which fails the condition (see execution_table row 3).
Does the CHECK constraint apply only when inserting data?
No, it applies both when inserting new data and when updating existing data to ensure the condition is always true.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at Step 3?
AThe data is inserted successfully
BThe data is rejected due to CHECK constraint
CThe table is dropped
DThe condition is ignored
💡 Hint
Check the 'Result' column in execution_table row 3
According to variable_tracker, how many rows are in the table after Step 3?
A0 rows
B2 rows
C1 row
D3 rows
💡 Hint
Look at the 'Employees Table Rows' after Step 3 in variable_tracker
If the CHECK constraint was changed to Age >= 16, what would happen at Step 3?
AThe insert would be allowed
BThe insert would still be rejected
CThe table would be deleted
DThe first insert would fail
💡 Hint
Refer to the condition column in execution_table and imagine changing 18 to 16
Concept Snapshot
CHECK constraint sets a rule on table columns.
It validates data on insert or update.
If data breaks the rule, database rejects it.
Syntax example: Age INT CHECK (Age >= 18).
Useful for data integrity and simple validations.
Full Transcript
The CHECK constraint is a rule added to a table column to ensure data meets certain conditions. When you insert or update data, the database checks if the data satisfies the condition. If it does, the data is accepted; if not, it is rejected. For example, a CHECK constraint on Age >= 18 means only ages 18 or older are allowed. In the example, inserting Age 20 succeeds, but Age 16 fails and is rejected. This helps keep data clean and valid automatically.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To enforce rules on column values to prevent invalid data -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    ALTER TABLE users ADD CONSTRAINT chk_age CHECK (age > 18); -> Option A
  4. 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]
Common Mistakes:
  • Omitting parentheses around condition
  • Not naming the constraint
  • Writing condition outside CHECK()
3. Given the table creation:
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
A. The row is inserted successfully
B. The price is automatically set to 0
C. The insert fails due to CHECK constraint violation
D. The insert causes a syntax error

Solution

  1. Step 1: Understand the CHECK constraint on price

    The constraint requires price to be greater than or equal to 0.
  2. Step 2: Analyze the insert statement

    The insert tries to add price = -10.00, which violates the CHECK condition.
  3. Final Answer:

    The insert fails due to CHECK constraint violation -> Option C
  4. Quick 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:
CREATE TABLE employees (
  id INT,
  salary INT CHECK salary > 0
);

But get a syntax error. What is the likely fix?
medium
A. Add a semicolon after salary
B. Add parentheses around the CHECK condition: CHECK (salary > 0)
C. Change salary to VARCHAR
D. Remove the CHECK constraint entirely

Solution

  1. Step 1: Identify syntax for inline CHECK constraints

    Inline CHECK constraints require the condition to be inside parentheses.
  2. Step 2: Check the given statement

    The statement misses parentheses around salary > 0, causing syntax error.
  3. Final Answer:

    Add parentheses around the CHECK condition: CHECK (salary > 0) -> Option B
  4. Quick 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
A. CHECK (discount >= 0 AND discount <= 50)
B. CHECK (discount > 0 AND discount < 50)
C. CHECK (discount BETWEEN 1 AND 50)
D. CHECK (discount != 0 OR discount != 50)

Solution

  1. Step 1: Understand the required range including boundaries

    The discount must be at least 0 and at most 50, so boundaries are included.
  2. 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.
  3. Final Answer:

    CHECK (discount >= 0 AND discount <= 50) -> Option A
  4. Quick 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