首页 > 代码库 > mysql5.1中delete和truncate的区别,实例如下
mysql5.1中delete和truncate的区别,实例如下
create table test_del(id int auto_increment,name varchar(10),primary key(id));insert into test_del(name) values(‘xuhao‘),(‘fdsa‘),(‘fddsf‘);create table test_truc(id int auto_increment,name varchar(10),primary key(id));insert into test_truc(name) values(‘xuhao‘),(‘fdsa‘),(‘fddsf‘);
mysql> select * from test_del;+----+-------+| id | name |+----+-------+| 1 | xuhao || 2 | fdsa || 3 | fddsf |+----+-------+3 rows in setmysql> select * from test_truc;+----+-------+| id | name |+----+-------+| 1 | xuhao || 2 | fdsa || 3 | fddsf |+----+-------+3 rows in setmysql> delete from test_del;Query OK, 3 rows affectedmysql> delete from test_truc where true;Query OK, 3 rows affectedmysql> insert into test_del(name) values(‘xuhao‘),(‘fdsa‘),(‘fddsf‘);Query OK, 3 rows affectedRecords: 3 Duplicates: 0 Warnings: 0mysql> insert into test_truc(name) values(‘xuhao‘),(‘fdsa‘),(‘fddsf‘);Query OK, 3 rows affectedRecords: 3 Duplicates: 0 Warnings: 0mysql> select * from test_del;+----+-------+| id | name |+----+-------+| 4 | xuhao || 5 | fdsa || 6 | fddsf |+----+-------+3 rows in setmysql> select * from test_truc;+----+-------+| id | name |+----+-------+| 4 | xuhao || 5 | fdsa || 6 | fddsf |+----+-------+3 rows in setmysql> truncate test_truc;Query OK, 0 rows affectedmysql> insert into test_truc(name) values(‘xuhao‘),(‘fdsa‘),(‘fddsf‘);Query OK, 3 rows affectedRecords: 3 Duplicates: 0 Warnings: 0mysql> select * from test_truc;+----+-------+| id | name |+----+-------+| 1 | xuhao || 2 | fdsa || 3 | fddsf |+----+-------+3 rows in set
对于表中的自增id,delete和truncate的处理方式是不同,另外据说delete操作会写入日志,truncate不写入日志,言下之意,truncate慎用,不易恢复。
mysql5.1中delete和truncate的区别,实例如下
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。