Learn SQL
-- constraints.check_constraints_deep

CHECK Constraints & Limits in SQL

Cheapest invariant — with a NULL loophole to close

--concept

CHECK enforces row predicates (salary >= 0, end > start) — rejected writes fail fast. Limits: no subqueries or cross-table refs (use triggers/deferred constraints), NULL passes (UNKNOWN is not FALSE — pair with NOT NULL), complex rules live in app validation plus the constraint as backstop. Every column-local rule belongs here.

--how it works
1

Row-local rules

salary >= 0, end > start — evaluated on INSERT/UPDATE, fail fast with named errors.

2

Span two columns

CONSTRAINT valid_range CHECK (end_time > start_time) — intra-row, not cross-table.

3

Know the walls

No subqueries/cross-table refs; NULL passes; complex belongs in app + trigger + constraint backstop.

4

Name + add safely

Named constraints greppable; dirty tables need clean/quarantine + NOT VALID/VALIDATE.

--visualization
ERROR: violates check "staff_salary_check"Refused regardless of writer (app/script/intern)
Stage 1 / 1
Row-local only — cross-table needs triggers. Named constraints are greppable.
--example
query.sql
1CREATE TABLE staff (id INT PRIMARY KEY, salary NUMERIC CHECK (salary >= 0));
2INSERT INTO staff VALUES (1, -5); -- rejected
Result
error
ERROR: new row violates check constraint "staff_salary_check"

Negative salary fails regardless of which app, script or intern sent it. NULL salary would still pass — the nullable-column loophole to close with NOT NULL.

--variants

Negative rejected everywhere

Engine refuses regardless of writer.

query.sql
1INSERT INTO staff (id, email, salary) VALUES (1, 'a@x.com', -5);
Result
error
ERROR: new row violates check constraint "staff_salary_check"
1App, script or intern — same refusal.
2UNIQUE on email would separately reject second a@x.com.
3DEFAULT would fill omitted bonus with 0.

Two-column range

End must exceed start on the same row.

query.sql
1CONSTRAINT valid_range CHECK (end_time > start_time)
Result
caseverdict
validend after start
invalidrejected at write
1Same-row comparison — CHECK’s ceiling.
2Cross-row (no overlapping bookings) needs exclusion constraints/triggers.
3NULL end passes unless NOT NULL paired.
--key facts
Scoperow-local predicates only
NULL passesUNKNOWN ≠ FALSE — add NOT NULL
Cheapest guardevery column-local rule as CHECK
--use cases
Money/range guards: non-negative, ordered intervals
Backstop behind friendly app validation
--under the hood
CHECK is a per-row CPU predicate — cheap, in-write-path; cross-table rules need triggers with concurrency care.
PG NOT VALID + VALIDATE splits lock: short catalog lock, long scan without blocking writes.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.