← Back to MySQL Course | Chapter 2: Databases & Tables | Lesson 6 of 7

TRUNCATE TABLE

TRUNCATE TABLE is like wiping everything off a chart but keeping the empty headings, so you can start filling it again from scratch.
Syntax
sql
TRUNCATE TABLE table_name;

Clearing Table Data

TRUNCATE TABLE removes every row while leaving the table's column structure intact, and it's typically much faster than DELETE because it skips row-by-row logging and trigger execution entirely.

Example: Clearing Table Data

sql
TRUNCATE TABLE users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Resetting Auto Increment Values

Unlike DELETE, which leaves the AUTO_INCREMENT counter wherever it stopped, TRUNCATE resets it back to its starting value — meaning the next inserted row gets ID 1 again, not a continuation of the old sequence.

Example: Resetting Auto Increment Values

sql
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
TRUNCATE TABLE users; -- next inserted row gets id 1 again

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Safe Data Cleanups

Because TRUNCATE can't be selectively filtered with a WHERE clause, it always empties the entire table — double-check you're connected to the right database before running it, since the operation can't be undone.

Example: Safe Data Cleanups

sql
-- TRUNCATE always empties the entire table; there is no WHERE clause
TRUNCATE TABLE users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Comparing Truncate Operations

TRUNCATE is technically a DDL (data definition language) statement, not DML — internally it drops and recreates the table structure rather than deleting rows one at a time, which explains its speed.

Example: Comparing Truncate Operations

sql
-- TRUNCATE is DDL: it drops and recreates the table structure, unlike DELETE
TRUNCATE TABLE users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Post-Truncate Verification

After truncating, a quick SELECT COUNT(*) confirms the table is empty and ready for fresh data, giving you confidence the reset actually completed as expected.

Example: Post-Truncate Verification

sql
TRUNCATE TABLE users;
SELECT COUNT(*) FROM users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting a WHERE clause with TRUNCATE TABLE, which does not allow it and always removes every row.
  2. Assuming TRUNCATE can be undone with ROLLBACK, when it is DDL and commits immediately.
  3. Running it on a table referenced by a foreign key from another table, which fails.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.