0
0
SQLquery~10 mins

CHECK constraint in SQL - Step-by-Step Execution

Choose your learning style9 modes available
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.