TRUNCATE

TRUNCATE 语句以非事务方式从表中删除所有数据。可认为 TRUNCATE 语句同 DROP TABLE + CREATE TABLE 组合在语义上相同,定义与 DROP TABLE 语句相同。

TRUNCATE TABLE tableNameTRUNCATE tableName 均为有效语法。

语法图

TruncateTableStmt:

TruncateTableStmt

OptTable:

OptTable

TableName:

TableName

示例

  1. CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY);
  1. Query OK, 0 rows affected (0.11 sec)
  1. INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
  1. Query OK, 5 rows affected (0.01 sec)
  2. Records: 5 Duplicates: 0 Warnings: 0
  1. SELECT * FROM t1;
  1. +---+
  2. | a |
  3. +---+
  4. | 1 |
  5. | 2 |
  6. | 3 |
  7. | 4 |
  8. | 5 |
  9. +---+
  10. 5 rows in set (0.00 sec)
  1. TRUNCATE t1;
  1. Query OK, 0 rows affected (0.11 sec)
  1. SELECT * FROM t1;
  1. Empty set (0.00 sec)
  1. INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
  1. Query OK, 5 rows affected (0.01 sec)
  2. Records: 5 Duplicates: 0 Warnings: 0
  1. TRUNCATE TABLE t1;
  1. Query OK, 0 rows affected (0.11 sec)

MySQL 兼容性

TRUNCATE 语句与 MySQL 完全兼容。如有任何兼容性差异,请在 GitHub 上提交 issue

另请参阅