Skip to content

Latest commit

 

History

History
115 lines (83 loc) · 1.87 KB

sql-statement-truncate.md

File metadata and controls

115 lines (83 loc) · 1.87 KB
title summary aliases
TRUNCATE
TiDB 数据库中 TRUNCATE 的使用概况。
/docs-cn/dev/sql-statements/sql-statement-truncate/
/docs-cn/dev/reference/sql/statements/truncate/

TRUNCATE

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

TRUNCATE TABLE tableNameTRUNCATE tableName 均为有效语法。

语法图

TruncateTableStmt ::=
    "TRUNCATE" ( "TABLE" )? TableName

TableName ::=
    (Identifier ".")? Identifier

示例

{{< copyable "sql" >}}

CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY);
Query OK, 0 rows affected (0.11 sec)

{{< copyable "sql" >}}

INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

{{< copyable "sql" >}}

SELECT * FROM t1;
+---+
| a |
+---+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+---+
5 rows in set (0.00 sec)

{{< copyable "sql" >}}

TRUNCATE t1;
Query OK, 0 rows affected (0.11 sec)

{{< copyable "sql" >}}

SELECT * FROM t1;
Empty set (0.00 sec)

{{< copyable "sql" >}}

INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

{{< copyable "sql" >}}

TRUNCATE TABLE t1;
Query OK, 0 rows affected (0.11 sec)

MySQL 兼容性

TRUNCATE 语句与 MySQL 完全兼容。如发现任何兼容性差异,请尝试 TiDB 支持资源

另请参阅