Skip to content
Learn Netverks

Lesson

Step 15/36 42% through track

constraints-sql

Constraints

Last reviewed May 28, 2026 Content v20260528
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches Constraints: the SQL patterns, schema habits, and query reasoning you need before advancing in SQL.

Teams query Constraints on every SQL codebase—skipping it leaves gaps in debugging and data reviews.

You will apply Constraints in contexts like: Postgres, MySQL, SQLite, warehouses, and ORMs that still expose SQL.

Copy SQL from each lesson into SQLite (sqlite3), DB Fiddle, or local Postgres—read result grids and row counts. The in-browser SQL lab (sql_sandbox) will run queries when the runner ships; until then, local clients are the practice path.

When you can explain the previous lesson's ideas without copying example queries verbatim.

Constraints encode business rules in the schema: uniqueness, allowed ranges, required fields, and valid references. They protect data quality when multiple apps write to the same database.

Common constraints

CREATE TABLE employees (
  id INTEGER PRIMARY KEY,
  email TEXT NOT NULL UNIQUE,
  department TEXT CHECK (department IN ('eng', 'sales', 'ops')),
  salary REAL CHECK (salary >= 0)
);

Practice: Run DDL in a fresh SQLite file or DB Fiddle schema pane. Drop test tables when experimenting.

NOT NULL and UNIQUE

NOT NULL rejects missing values. UNIQUE rejects duplicate combinations—NULL handling in UNIQUE varies slightly by engine (Postgres allows multiple NULLs in unique columns).

CHECK in practice

INSERT INTO employees (email, department, salary)
VALUES ('a@x.com', 'eng', 80000);  -- ok

-- Fails CHECK:
INSERT INTO employees (email, department, salary)
VALUES ('b@x.com', 'legal', 80000);

Complex rules may live in application validation too—constraints are the last line of defense.

Important interview questions and answers

  1. Q: CHECK vs app validation?
    A: Both—constraints catch bugs from any client; apps give friendlier error messages.
  2. Q: UNIQUE on (a, b)?
    A: Pair must be unique together; either column can repeat independently.

Self-check

  1. Which constraint ensures email appears only once?
  2. What does CHECK validate?

Tip: CHECK constraints catch bad data from any client, not just your app validation.

Interview prep

UNIQUE vs PRIMARY KEY?

Both unique; PK is main identifier and NOT NULL.

CHECK constraint?

Validates row data against a boolean expression.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • CHECK vs trigger?
  • UNIQUE scope?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump