CMD Guide
HomeDatabasesSQL Fundamentals

TRUNCATE

What TRUNCATE does — and what it doesn't

TRUNCATE TABLE t removes all rows from a table while keeping the table itself — its columns, data types, indexes, and constraints all remain. (The earlier claim that it "removes rows and columns" is wrong: columns are part of the structure, and the structure is retained — that's the whole point versus DROP.)

TRUNCATE (DDL, page-deallocation, fast, usually non-rollback) versus DELETE (DML, per-row, WHERE-able, rollback-able); both retain the table structure
TRUNCATE (DDL, page-deallocation, fast, usually non-rollback) versus DELETE (DML, per-row, WHERE-able, rollback-able); both retain the table structure

Why it's fast, and the rollback nuance

TRUNCATE is fast not "because it only deals with data" but because of how it removes that data: instead of deleting and logging each row (as DELETE does), it deallocates the table's data pages wholesale with minimal logging, and resets the AUTO_INCREMENT counter. It takes no WHERE clause (all or nothing) and does not fire per-row triggers.

Rollback is engine-specific — a real gotcha:

TRUNCATEDELETEDROP
Removesall rowsrows (WHERE)the whole table
Keeps structure?yesyesno
ClassDDLDMLDDL
Rollback (MySQL)noyesno

Takeaways


Re-authored for correctness for this guide (the prior version said TRUNCATE removes columns and could always be rolled back). Per MySQL, PostgreSQL, Oracle & SQL Server docs. See also: DELETE, What is SQL (DDL vs DML).

🎯 STRICT STANDOUT: Why / mental model / when-not / worked / failure / hostile panel — TRUNCATE

Why this concept exists (judgment layer)

TRUNCATE is page-deallocation DDL (usually), not 'fast DELETE'. Engine-specific rollback and FK refusal are the production landmines — MySQL cannot undo it.

Mental model (install this intuition)

TRUNCATE keeps structure, drops all rows, minimal logging, resets identity, skips row triggers. MySQL/Oracle: implicit commit, no ROLLBACK. Postgres/SQL Server: transactional TRUNCATE. Refuses if FK references remain (unless CASCADE).

Worked example with numbers or traced steps

DELETE 10M rows: row locks + undo + VACUUM later; identity unchanged
TRUNCATE: deallocate pages; AUTO_INCREMENT → 1; seconds not hours
MySQL: BEGIN; TRUNCATE t; ROLLBACK;  -- still empty (already committed)
Postgres: BEGIN; TRUNCATE t; ROLLBACK; -- rows back
FK child exists → ERROR unless TRUNCATE … CASCADE

When NOT to use / named alternative

Need WHERE filter, row triggers, or safe rollback on MySQL → DELETE. Need table definition gone → DROP. Need partial purge → batched DELETE or partition DROP.

Failure mode & ops fingerprint

Fingerprint: engineer wraps TRUNCATE in transaction on MySQL expecting undo; prod table emptied permanently; TRUNCATE blocked by FK; surprise identity reset breaks external references that assumed stable ids.

Hostile-panel drills (defend the decision)

Q1. Does TRUNCATE remove columns?
Model answer: No — that myth is wrong. Structure (columns, indexes, constraints) remains; only rows go.

Q2. Rollback TRUNCATE on MySQL InnoDB?
Model answer: No — DDL auto-commits. Treat as irreversible.

Q3. Why is TRUNCATE faster than DELETE FROM t?
Model answer: Page deallocation with minimal logging vs per-row undo/redo and later vacuum/purge.

🤖 Don't fully get this? Learn it with Claude

Stuck on TRUNCATE? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **TRUNCATE** (Databases) and want to truly understand it. Explain TRUNCATE from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **TRUNCATE** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **TRUNCATE** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **TRUNCATE** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes