TRUNCATE

The TRUNCATE statement removes all data from the table in a non-transactional way. TRUNCATE can be thought of as semantically the same as DROP TABLE + CREATE TABLE with the previous definition.

Both TRUNCATE TABLE tableName and TRUNCATE tableName are valid syntax.

Synopsis

TruncateTableStmt

TRUNCATE - 图1

TableName

TRUNCATE - 图2

  1. TruncateTableStmt ::=
  2. "TRUNCATE" ( "TABLE" )? TableName
  3. TableName ::=
  4. (Identifier ".")? Identifier

Examples

  1. mysql> CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY);
  2. Query OK, 0 rows affected (0.11 sec)
  3. mysql> INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
  4. Query OK, 5 rows affected (0.01 sec)
  5. Records: 5 Duplicates: 0 Warnings: 0
  6. mysql> SELECT * FROM t1;
  7. +---+
  8. | a |
  9. +---+
  10. | 1 |
  11. | 2 |
  12. | 3 |
  13. | 4 |
  14. | 5 |
  15. +---+
  16. 5 rows in set (0.00 sec)
  17. mysql> TRUNCATE t1;
  18. Query OK, 0 rows affected (0.11 sec)
  19. mysql> SELECT * FROM t1;
  20. Empty set (0.00 sec)
  21. mysql> INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
  22. Query OK, 5 rows affected (0.01 sec)
  23. Records: 5 Duplicates: 0 Warnings: 0
  24. mysql> TRUNCATE TABLE t1;
  25. Query OK, 0 rows affected (0.11 sec)

MySQL compatibility

The TRUNCATE statement in TiDB is fully compatible with MySQL. If you find any compatibility differences, report a bug.

See also