// tables reference

SQL TRUNCATE TABLE

Removes every row without visiting them one by one, usually by dropping and recreating the storage. It is DDL in most engines: faster than DELETE, harder to undo, and stricter about foreign keys.

Kategorie
Tabellen
In this category
5 of 16

Syntax

TRUNCATE TABLE table_name
TRUNCATE TABLE t1, t2 [RESTART IDENTITY | CONTINUE IDENTITY] [CASCADE]  -- PostgreSQL
DELETE FROM table_name  -- SQLite has no TRUNCATE

Each block is a separate form. The support table and the examples show which engine takes which.

Engine-Unterstützung

EngineSupportNotizen
SQL Server Ja resets IDENTITY to the seed; refuses while any foreign key references the table
PostgreSQL Ja several tables at once; RESTART IDENTITY; CASCADE empties referencing tables too
MySQL Ja implicit commit; resets AUTO_INCREMENT; refuses on InnoDB tables a foreign key points at
SQLite No equivalent no TRUNCATE; DELETE FROM t does the same job
Oracle Ja DDL with implicit commit; no flashback query afterwards; CASCADE empties referencing tables

Beispiele

PostgreSQL

TRUNCATE TABLE stage_orders RESTART IDENTITY;

SQL Server

TRUNCATE TABLE dbo.stage_orders;

MySQL

TRUNCATE TABLE stage_orders; -- implicit commit: no rollback

SQLite

-- no TRUNCATE in SQLite
DELETE FROM stage_orders;

Oracle

TRUNCATE TABLE stage_orders;

Gut zu wissen

  • Foreign keys gate the statement: SQL Server and MySQL refuse to truncate any table another table's foreign key points at, even when the referencing table is empty (a foreign key from the table to itself is allowed). PostgreSQL refuses too, empty or not, unless you list the referencing tables in the same TRUNCATE or add CASCADE, which truncates them too.
  • Rollback rules differ by engine. SQL Server, PostgreSQL and SQLite run inside a transaction and roll back cleanly. MySQL and Oracle commit implicitly around TRUNCATE, so there is no undo once it runs (and no flashback query on Oracle afterwards).
  • TRUNCATE resets the auto-increment counter on SQL Server and MySQL. PostgreSQL keeps counting unless you add RESTART IDENTITY. If ids must keep climbing after an empty-and-reload, use DELETE, or RESTART IDENTITY deliberately.
  • SQLite has no TRUNCATE. DELETE FROM t with no WHERE removes every row and gets SQLite's truncate optimization, which skips the row-by-row walk. A table using AUTOINCREMENT keeps its counter in sqlite_sequence, so ids continue unless you delete the row there too.

Questions about TRUNCATE TABLE

Is TRUNCATE faster than DELETE?

Usually, yes: it releases the storage without visiting each row and logs far less. DELETE also fires delete triggers and never resets the identity counter; TRUNCATE skips the triggers and, on SQL Server and MySQL, resets the counter. For a few hundred rows the difference is noise; for millions it is seconds versus minutes.

Why can I not truncate my table?

A foreign key somewhere references it. SQL Server and MySQL refuse while the constraint exists, even against an empty child table; PostgreSQL and Oracle offer CASCADE, which empties the referencing tables as well.

Does TRUNCATE reset the identity counter?

Yes on SQL Server and MySQL. PostgreSQL needs RESTART IDENTITY; without it the counter continues. SQLite has no counter to reset unless the table used AUTOINCREMENT, whose value lives in sqlite_sequence.

Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.

// dialect-aware editing

DDL that matches the engine you're connected to

SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.