Learn SQL
-- constraints.not_null_default_deep

NOT NULL & DEFAULT in Depth in SQL

Every column deserves an explicit NULL verdict

--concept

Nullable-by-default means every column is a NULL decision you must make: ids, names, amounts are NOT NULL; optional notes stay nullable. DEFAULT supplies omitted-insert values (0, '', now(), generated expressions) so writers skip repetition. Required facts + defaults close the mystery-NULL factory;ower layers (CHECK) then assume presence.

--how it works
1

Verdict per column

Ids/names/amounts NOT NULL; notes nullable. Implicit nullability breeds mystery NULLs.

2

DEFAULT fills omission

0, now(), generated — writers skip repetition, old rows stay valid on ADD.

3

Backfill then constrain

Full tables: nullable → batch-fill → SET NOT NULL. Direct fails or long-locks.

4

Pair with CHECK

CHECK lets NULLs through (UNKNOWN). NOT NULL closes the loophole.

--visualization
columnrule
nameNOT NULL
bonusDEFAULT 0
created_atNOT NULL + now()
Stage 1 / 1
CHECK lets NULLs through (UNKNOWN) — NOT NULL closes it. ADD fills old rows via DEFAULT.
--example
query.sql
1CREATE TABLE staff (
2 id INT PRIMARY KEY,
3 name TEXT NOT NULL,
4 bonus NUMERIC(12,2) DEFAULT 0,
5 created_at TIMESTAMPTZ NOT NULL DEFAULT now()
6);
Result
columnrule
nameNOT NULL — required
bonusDEFAULT 0
created_atDEFAULT now()

Inserting (1, 'Ada') fills bonus 0 and now() automatically. Inserting NULL name fails fast with a clear error instead of a downstream mystery.

--variants

Staff DDL with verdicts

Required facts + filled optionals.

query.sql
1CREATE TABLE staff (
2 id INT PRIMARY KEY,
3 name TEXT NOT NULL,
4 bonus NUMERIC DEFAULT 0,
5 created_at TIMESTAMPTZ NOT NULL DEFAULT now()
6);
Result
columnrule
nameNOT NULL
bonusDEFAULT 0
created_atNOT NULL + now()
1Omitting bonus inserts 0, not NULL.
2Omitting created_at stamps now().
3NULL name errors fast with a clear message.

CHECK needs NOT NULL

NULL sails through CHECK alone.

query.sql
1salary NUMERIC CHECK (salary >= 0) -- NULL passes!
2name TEXT NOT NULL CHECK (length(name) > 0) -- closed
Result
formeffect
CHECK aloneNULL passes (UNKNOWN)
CHECK + NOT NULLenforced
1UNKNOWN is not FALSE — CHECK accepts it.
2NOT NULL rejects before CHECK evaluates.
3Every constrained column gets both verdicts.
--key facts
NOT NULLbans missing — required facts
DEFAULTfills omitted — constants without repetition
CHECK + NOT NULLCHECK alone lets NULLs through
--use cases
New-table DDL reviews: nullability line-by-line
Dirty-table migrations: backfill contracts
--under the hood
NOT NULL is a metadata flag — cheap to check; DEFAULT on ADD is metadata-only in modern PG (no rewrite).
Generated columns (GENERATED ALWAYS AS) compute defaults deterministically and indexably.

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.