4.4.3 · D3Databases

Worked examples — SQL DDL — CREATE, ALTER, DROP, TRUNCATE

3,717 words17 min readBack to topic

The scenario matrix

Every DDL problem is really one of these case classes. Think of the matrix like the four quadrants of a graph: each cell is a distinct situation with its own rule, and if you skip a cell you'll hit it unprepared in real life. The "Example / Case" column below is the anchor: Example n on this page = Case letter, so "Example 6" and "Case F" are the same worked example — the letter names the concept, the number names its position.

# Case class What makes it tricky Worked in
A CREATE from scratch, empty world nothing to conflict with — but order matters (parent before child) Example 1 = Case A
B ALTER on an empty table no rows to violate rules → almost anything allowed Example 2 = Case B
C ALTER on a populated table existing rows can violate a new constraint Example 3 = Case C
D DROP with no dependents clean removal; irreversibility is engine-specific Example 4 = Case D
E DROP with a foreign-key dependent (referential block) DB refuses; CASCADE needed Example 5 = Case E
F TRUNCATE + attempted ROLLBACK (auto-commit trap) rows do not come back [MySQL/InnoDB] Example 6 = Case F
G TRUNCATE blocked by a foreign key won't run at all Example 7 = Case G
H Degenerate / zero-effect input (IF EXISTS on an absent table) must produce no error, no change Example 8 = Case H
I Real-world word problem (renovate a live schema safely) combine ALTER + DEFAULT + backup thinking Example 9 = Case I
J Exam twist (spot the difference: DELETE vs TRUNCATE vs DROP) choosing the right verb under constraints Example 10 = Case J

We'll walk the whole ecosystem below. Keep this figure in mind — it shows the two tables and the arrow (the foreign key) that ties them together, because most "tricky" cases are about that arrow.


Case A (Example 1) — CREATE in an empty world (order matters)


Case B (Example 2) — ALTER an empty table (freedom)


Case C (Example 3) — ALTER a populated table (rows fight back)


Case D (Example 4) — DROP with no dependents (clean, irreversibility depends on engine)


Case E (Example 5) — DROP blocked by a foreign key (the referential wall)


Case F (Example 6) — TRUNCATE then ROLLBACK (the auto-commit trap) [MySQL/InnoDB]


Case G (Example 7) — TRUNCATE blocked by a foreign key


Case H (Example 8) — Degenerate input: DROP something that isn't there


Case I (Example 9) — Real-world word problem: renovate a live table safely


Case J (Example 10) — Exam twist: pick the right verb under constraints

Recall Rebuild the whole matrix from memory

Which cell (Example/Case) needs a DEFAULT? ::: Example 3 = Case C — ALTER adding NOT NULL to a populated table. Which two cells involve a foreign key blocking you? ::: Example 5 = Case E (DROP) and Example 7 = Case G (TRUNCATE). Which cell proves TRUNCATE can't be rolled back — and on which engine? ::: Example 6 = Case F, specifically MySQL/InnoDB (PostgreSQL can roll it back inside a transaction). Which verb is the answer when you must filter rows? ::: DELETE (Example 10 = Case J) — it's DML, not DDL. What keyword makes a DROP script idempotent? ::: IF EXISTS (Example 8 = Case H).


Recall One-line summary of the whole page

The trickiness of any DDL statement comes from three forces: existing rows (Cases B/C), the foreign-key arrow (Cases A/E/G), and auto-commit — which is real on MySQL/InnoDB but relaxed on PostgreSQL (Cases D/F). Master those three and every cell is just a combination.

See also: Constraints — PRIMARY KEY, FOREIGN KEY, CHECK, UNIQUE, Normalization, Indexes, Referential Integrity & CASCADE.