Changing a schema over time

Lesson 9 of 18 · SQL foundations

A migration is an ordered, recorded schema change shared with application code. An up migration moves forward; a down migration attempts to reverse it. A rollback may discard newly entered data and cannot necessarily restore lost values. Use backups and test the rollout and recovery on representative copies.

In a fresh setup, the following adds and then removes a column (SQLite 3.35+ supports DROP COLUMN with restrictions):

ALTER TABLE books ADD COLUMN language TEXT;
ALTER TABLE books DROP COLUMN language;

SQLite can also rename a table or column; more complex changes often require rebuilding a table. A migration tool can record which versions ran, but does not make changes safe by itself. For deployed applications, first add compatible structures, update readers and writers, backfill and verify data, then remove old structures only after dependent versions have retired. Timing, locks, and rollback plans depend on the actual system.

Chapter index · Course index