TRUNCATE [TABLE] tbl_name
TRUNCATE TABLE
empties a table completely. TRUNCATE TABLE
完全清空表。It requires the 它需要DROP
privilege. DROP
特权。Logically, 从逻辑上讲,TRUNCATE TABLE
is similar to a DELETE
statement that deletes all rows, or a sequence of DROP TABLE
and CREATE TABLE
statements.TRUNCATE TABLE
类似于删除所有行的DELETE
语句,或者类似于DROP TABLE
和CREATE TABLE
的语句序列。
To achieve high performance, 为了获得高性能,TRUNCATE TABLE
bypasses the DML method of deleting data. TRUNCATE TABLE
绕过了删除数据的DML方法。Thus, it does not cause 因此,它不会引发ON DELETE
triggers to fire, it cannot be performed for InnoDB
tables with parent-child foreign key relationships, and it cannot be rolled back like a DML operation. ON DELETE
触发器,不能对具有父子外键关系的InnoDB
表执行,也不能像DML操作那样回滚。However, 但是,对使用原子DDL支持的存储引擎的表执行的TRUNCATE TABLE
operations on tables that use an atomic DDL-supported storage engine are either fully committed or rolled back if the server halts during their operation. TRUNCATE TABLE
操作要么是完全提交的,要么是在操作期间服务器停止时回滚的。For more information, see Section 13.1.1, “Atomic Data Definition Statement Support”.有关更多信息,请参阅第13.1.1节,“原子数据定义语句支持”。
Although 尽管TRUNCATE TABLE
is similar to DELETE
, it is classified as a DDL statement rather than a DML statement. TRUNCATE TABLE
类似于DELETE
,但它被分类为DDL语句而不是DML语句。It differs from 它与DELETE
in the following ways:DELETE
的不同之处在于:
Truncate operations drop and re-create the table, which is much faster than deleting rows one by one, particularly for large tables.截断操作删除并重新创建表,这比逐个删除行快得多,特别是对于大型表。
Truncate operations cause an implicit commit, and so cannot be rolled back. 截断操作导致隐式提交,因此无法回滚。See Section 13.3.3, “Statements That Cause an Implicit Commit”.请参阅第13.3.3节,“导致隐式提交的语句”。
Truncation operations cannot be performed if the session holds an active table lock.如果会话持有活动表锁,则无法执行截断操作。
对于TRUNCATE TABLE
fails for an InnoDB
table or NDB
table if there are any FOREIGN KEY
constraints from other tables that reference the table. InnoDB
表或NDB
表,如果存在来自引用该表的其他表的任何外键约束,则TRUNCATE TABLE
失败。Foreign key constraints between columns of the same table are permitted.允许在同一表的列之间使用外键约束。
Truncation operations do not return a meaningful value for the number of deleted rows. 截断操作不会为删除的行数返回有意义的值。The usual result is “0 rows affected,” which should be interpreted as “no information.”通常的结果是“0行受影响”,应解释为“无信息”。
As long as the table definition is valid, the table can be re-created as an empty table with 只要表定义有效,就可以使用TRUNCATE TABLE
, even if the data or index files have become corrupted.TRUNCATE TABLE
将表重新创建为空表,即使数据或索引文件已损坏。
Any 任何AUTO_INCREMENT
value is reset to its start value. AUTO_INCREMENT
值都将重置为其起始值。This is true even for 甚至对于MyISAM
and InnoDB
, which normally do not reuse sequence values.MyISAM
和InnoDB
也是如此,它们通常不重用序列值。
When used with partitioned tables, 当与分区表一起使用时,TRUNCATE TABLE
preserves the partitioning; that is, the data and index files are dropped and re-created, while the partition definitions are unaffected.TRUNCATE TABLE
保留分区;也就是说,删除并重新创建数据和索引文件,而分区定义不受影响。
The TRUNCATE TABLE
statement does not invoke ON DELETE
triggers.TRUNCATE TABLE
语句不会调用ON DELETE
触发器。
Truncating a corrupted 支持截断损坏的InnoDB
table is supported.InnoDB
表。
出于二进制日志记录和复制的目的,TRUNCATE TABLE
is treated for purposes of binary logging and replication as DDL rather than DML, and is always logged as a statement.TRUNCATE TABLE
被视为DDL而不是DML,并且总是作为语句记录。
针对表的TRUNCATE TABLE
for a table closes all handlers for the table that were opened with HANDLER OPEN
.TRUNCATE TABLE
关闭在HANDLER OPEN
中打开的表的所有处理程序。
In MySQL 5.7 and earlier, on a system with a large buffer pool and 在MySQL5.7及更早版本中,在启用了大型缓冲池和innodb_adaptive_hash_index
enabled, a TRUNCATE TABLE
operation could cause a temporary drop in system performance due to an LRU scan that occurred when removing the table's adaptive hash index entries (Bug #68184). innodb_adaptive_hash_index
的系统上,由于删除表的自适应哈希索引项时发生的LRU扫描,TRUNCATE TABLE
操作可能会导致系统性能临时下降(错误Š68184)。The remapping of MySQL 8.0中TRUNCATE TABLE
to DROP TABLE
and CREATE TABLE
in MySQL 8.0 avoids the problematic LRU scan.TRUNCATE TABLE
到DROP TABLE
和CREATE TABLE
的重新映射避免了有问题的LRU扫描。
TRUNCATE TABLE
can be used with Performance Schema summary tables, but the effect is to reset the summary columns to 0 or NULL
, not to remove rows. TRUNCATE TABLE
可用于性能架构摘要表,但其效果是将摘要列重置为0或NULL
,而不是删除行。See Section 27.12.20, “Performance Schema Summary Tables”.请参阅第27.12.20节,“性能模式汇总表”。
Truncating an 截断驻留在每个表空间的文件中的InnoDB
table that resides in a file-per-table tablespace drops the existing tablespace and creates a new one. InnoDB
表会删除现有的表空间并创建一个新的表空间。As of MySQL 8.0.21, if the tablespace was created with an earlier version and resides in an unknown directory, 从MySQL 8.0.21开始,如果表空间是用早期版本创建的,并且驻留在未知目录中,InnoDB
creates the new tablespace in the default location and writes the following warning to the error log: InnoDB
将在默认位置创建新表空间,并将以下警告写入错误日志:The DATA DIRECTORY location must be in a known directory. 数据目录位置必须在已知目录中。The DATA DIRECTORY location will be ignored and the file will be put into the default datadir location. 数据目录位置将被忽略,文件将放入默认的datadir位置。Known directories are those defined by the 已知目录是由datadir
, innodb_data_home_dir
, and innodb_directories
variables. datadir
、innodb_data_home_dir
和innodb_directories
变量定义的目录。To have 要让TRUNCATE TABLE
create the tablespace in its current location, add the directory to the innodb_directories
setting before running TRUNCATE TABLE
.TRUNCATE TABLE
在其当前位置创建表空间,请在运行TRUNCATE TABLE
之前将目录添加到innodb_directories
设置中。