MySQL_TRUNCATE_清空table里的记录

MySQL TRUNCATE 语法

TRUNCATE [TABLE] tbl_name

TRUNCATE TABLE empties a table completely. It requires the DROP privilege.

Logically, TRUNCATE TABLE is similar to a DELETE statement that deletes all rows, or a sequence of DROP TABLE and CREATE TABLE statements. To achieve high performance, it bypasses the DML method of deleting data. Thus, it cannot be rolled back, it does not cause ON DELETE triggers to fire, and it cannot be performed for InnoDB tables with parent-child foreign key relationships.

Although TRUNCATE TABLE is similar to DELETE, it is classified as a DDL(数据库定义语言) statement rather than a DML(数据操作语言) statement. It differs from DELETE in the following ways in MySQL 5.7:

  • 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.
  • Truncation operations cannot be performed if the session holds an active table lock.
  • TRUNCATE TABLE fails for an InnoDB table if there are any FOREIGN KEY constraints from other tables that reference the 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.”
  • As long as the table format file tbl_name.frm 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(败坏的).
  • Any AUTO_INCREMENT value is reset to its start value. This is true even for MyISAM and InnoDB, which normally do not reuse sequence values.
  • 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 (.par) file is unaffected.
  • The TRUNCATE TABLE statement does not invoke ON DELETE triggers.

TRUNCATE TABLE for a table closes all handlers for the table that were opened with HANDLER OPEN.

TRUNCATE TABLE is treated for purposes of binary logging and replication as DROP TABLE followed by CREATE TABLE—that is, as DDL rather than DML. This is due to the fact that, when using InnoDB and other transactional storage engines where the transaction isolation level does not permit statement-based logging (READ COMMITTED or READ UNCOMMITTED), the statement was not logged and replicated when using STATEMENT or MIXED logging mode. (Bug #36763) However, it is still applied on replication slaves using InnoDB in the manner described previously.

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

有两个表t_parent和t_child,t_child表中有一个外键,关联t_parent

TRUNCATE t_parent;

执行这条语句报错:

[TRUNCATE - 0 row(s), 0.000 secs]  [Error Code: 1701, SQL State: 42000]  Cannot truncate a table referenced in a foreign key constraint (`test`.`t_child`, CONSTRAINT `FK_t_child` FOREIGN KEY (`parent_id`) REFERENCES `test`.`t_parent` (`id`))

Code: 1701 SQL State: HY000 --- Cannot truncate a table referenced in a foreign key constraint (`test`.`t_child`, CONSTRAINT `FK_t_child` FOREIGN KEY (`parent_id`) REFERENCES `test`.`t_parent` (`id`))

... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec  [0 successful, 0 warnings, 1 errors]

因为有外键关联

SET FOREIGN_KEY_CHECKS=0;
TRUNCATE t_parent;
select count(*) from t_parent;

最后查出来的结果是0,首先可以取消外键关联,就可以TRUNCATE 表。。。。

============END==========

时间: 2024-11-12 03:57:21

MySQL_TRUNCATE_清空table里的记录的相关文章

关于table的一些记录

HTML有10个表格相关标签 <caption> 表格的大标题,该标记可以出现在<table> 之间的任意位置.它对于搜索引擎的机器人记录信息十分重要.参数有align.valign <col> 表格的列定义属性 <colgroup> 定义表格列的分组,Firefox.Chrome 以及Safari 仅支持colgroup 元素的span 和width 属性 <table> 定义表格 <tbody> 定义表格主体 <td>

JDBC访问Oracle数据库例子源代码,包括创建table,删除table,插入记录,删除记录,查询记录等

package com.cb; public class SMSInfo { public static String ITEMINDEX = "sms_index"; public static String ITEMTO = "sms_to"; public static String ITEMFROM = "sms_from"; public static String ITEMMSG = "sms_msg"; publ

Table里嵌套ASPXGridView

Table里嵌套ASPXGridView 简述 有时候我们在录入数据的时候,因为录入数据字段比较少,所以可以直接在GridView上录入. 但是我们有些公用字段是在单头中固定的,GridView显示的是单身. 这样为了方便我们把GridView嵌套在Table中,录入数据时就可以取到单头上的数据. 也可以根据单头来检索同类数据,编辑数据也可在GridView上操作. ASPXGridView 这边在GridView是选择了DevExpress控件里的ASPXGridView. 这个第三方控件是美

element-ui 解决 table 里包含表单验证的问题!

实际项目中的场景,需要在table里做表单的验证,如图效果: 其实问题关键就在于如何给el-form-item动态绑定prop :prop="'tableData.' + scope.$index + '.字段名'" 详见代码: <template> <div v-bgb-block> <div style="margin-top:10px;"> <el-form :rules="model.rules"

django模板里循环变量&lt;table&gt;里想要两个一行如何控制

2016-8-3 周三 做项目时遇到的问题: 每个div由循环变量输出: {% for key,value in formextenddetail %} <div id="div_id_notes" class="value form-group row"> <div class="control-label">{{ key }}</div> <div class="controls"

MYSQL碰到The total number of locks exceeds the lock table size 问题解决记录

解决记录如下: 在mysql里面进行修改操作时提示:The total number of locks exceeds the lock table size ,通过百度搜到innodb_buffer_pool_size过小: 打开mysql 命令框 输入 show variables like "%tmp%"; 查看innodb_buffer_pool_size,输入SET GLOBAL innodb_buffer_pool_size=67108864; 完成之后再次使用show v

Table里td中的文本过长,设置不换行,随内容同行显示(转载)

当td中内容过长时,内容会溢出,换行显示,美观超级差,在td里设置这个属性 "white-space:nowrap   就可以解决排版问题啦 <td style="white-space:nowrap">非法闯入闯出亮灯处理:</td> 不美观的样式: 美观的样式: 另外,nowrap属性可以用在很多标签里面哦,美化排版效果.

【JavaScript】table里面点击某td获取同一行tr的其他td值

某td的input(保存按钮)上绑定方法,点击按钮保存该行所有数据 function locationedit(num){ var ordernumber = $("#"+num).parent().parent().find("td").eq(1).text(); var itemnumber = $("#"+num).parent().parent().find("td").eq(4).text(); var locati

C#清空datagirdview里的数据

C# DataGridView控件绑定数据后清空数据在清除DataGridview的数据时: 1.DataSource为NULL(DataGridView.DataSource= null;)这样会将DataGridView的列也删掉. 2.用DataGridview.Rows.Clear();  提示"不能清除此列表"!!!!! 以上都不是想要的结果. 想要满足保持原有的列,就是重新绑定之前的DataTable,然后清除DataTable中的数据,如下: DataTable  dt