CHECK constraint in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use a CHECK constraint in SQL, the database checks if data meets certain rules before saving it.
We want to know how the time to check data grows as we add more rows.
Analyze the time complexity of the following code snippet.
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Age INT,
Salary DECIMAL(10,2),
CHECK (Age >= 18 AND Salary >= 0)
);
INSERT INTO Employees (ID, Age, Salary) VALUES (1, 25, 50000);
INSERT INTO Employees (ID, Age, Salary) VALUES (2, 17, 40000); -- This will fail
This code creates a table with a CHECK constraint to ensure Age is at least 18 and Salary is not negative.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database checks the constraint for each new row inserted or updated.
- How many times: Once per row operation, so the check runs for every row added or changed.
Each time we add a row, the database runs the CHECK condition once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of rows inserted or updated.
Time Complexity: O(n)
This means the time to check constraints grows in a straight line as you add more rows.
[X] Wrong: "The CHECK constraint runs once for the whole table regardless of rows."
[OK] Correct: The database checks each row individually when it is inserted or updated, so the time grows with the number of rows.
Understanding how constraints affect performance helps you design databases that keep data correct without slowing down too much.
"What if we added a CHECK constraint that involves a subquery? How would the time complexity change?"
Practice
CHECK constraint in SQL?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]
- Confusing CHECK with indexing or keys
- Thinking CHECK creates backups
- Assuming CHECK improves speed
age to allow only values greater than 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 AQuick Check:
ADD CONSTRAINT name CHECK (condition) = ALTER TABLE users ADD CONSTRAINT chk_age CHECK (age > 18); [OK]
- Omitting parentheses around condition
- Not naming the constraint
- Writing condition outside CHECK()
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);
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]
- Assuming negative values are accepted
- Thinking CHECK auto-corrects values
- Confusing CHECK violation with syntax error
CREATE TABLE employees ( id INT, salary INT CHECK salary > 0 );
But get a syntax error. What is the likely fix?
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]
- Omitting parentheses in inline CHECK
- Changing data type unnecessarily
- Ignoring syntax error details
discount column in orders table is between 0 and 50 inclusive. Which CHECK constraint correctly enforces this?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]
- Using > and < excludes boundary values
- Using incorrect bounds with BETWEEN
- Logical errors with OR instead of AND
