Learn SQL
-- tables.alter_table

ALTER TABLE — Evolving Schema in SQL

Add columns safely, change types carefully

--concept

ALTER TABLE changes structure after creation: ADD COLUMN (with DEFAULT so old rows stay valid), DROP COLUMN, ALTER COLUMN TYPE (may rewrite the table), RENAME, ADD/DROP CONSTRAINT. Big-table alters can lock — production prefers online variants, backfill in batches, and testing on a copy first.

--how it works
1

ADD with DEFAULT

New column arrives populated for old rows — no NULL surprise.

2

Backfill then constrain

NOT NULL on a full table: add nullable → batch-fill → SET NOT NULL.

3

TYPE changes rewrite

Widening types can rewrite every row — off-peak + copy-tested.

4

Constraints validate history

ADD CHECK/UNIQUE scans old rows first. PG low-lock: NOT VALID then VALIDATE.

--visualization
Step 1ADD (default fills)Step 2BACKFILL (batches)Step 3CONSTRAIN (fast)nullable → fill → constrain (never direct NOT NULL on dirty tables)
Stage 1 / 3
Step 1: ADD COLUMN bonus DEFAULT 0 — old rows arrive populated, stay valid.
--example
query.sql
1ALTER TABLE employees ADD COLUMN bonus NUMERIC(12,2) DEFAULT 0;
2ALTER TABLE employees ALTER COLUMN salary TYPE NUMERIC(14,2);
Result
idnamesalarybonus
1Alice850005000
2Bob520000

Bonus arrives defaulted to 0 for existing rows (no NULL surprise). The type widening from (10,2) to (14,2) rewrites the column to hold larger payrolls.

--variants

Safe column add

Old rows stay valid because the default fills them.

query.sql
1ALTER TABLE employees ADD COLUMN bonus NUMERIC(12,2) DEFAULT 0;
Result
idnamebonus
1Alice5000
2Bob0
1DDL appends the column with 0 for the six existing rows.
2New INSERTs omitting bonus also get 0.
3No backfill job needed for this shape.

NOT NULL on dirty table

Three-step dance avoids the failed ALTER.

query.sql
1-- 1. add nullable
2ALTER TABLE employees ADD COLUMN band TEXT;
3-- 2. backfill in batches
4UPDATE employees SET band = 'junior' WHERE band IS NULL;
5-- 3. constrain
6ALTER TABLE employees ALTER COLUMN band SET NOT NULL;
Result
patterndetail
stepnullable → fill → constrain
1Direct ADD NOT NULL on a full table fails or locks long.
2Batched backfill keeps replication close.
3SET NOT NULL is then a metadata check, fast.
--key facts
Safe addDEFAULT keeps old rows valid
NOT NULL backfillnullable → fill → constrain, never direct on dirty tables
Lock costtype changes and constraint adds can lock — test on a copy
--use cases
Zero-downtime migrations: expand-contract column adds
Type widening before payroll overflow
--under the hood
PG 11+ adds DEFAULT columns without rewrite (metadata-only); TYPE changes still rewrite.
NOT VALID + VALIDATE splits the lock: short write-lock, long validating 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.