清空表情况:
mysql> delete from t20; Query OK, 7 rows affected (0.00 sec) mysql> show create table t20\G; *************************** 1. row *************************** Table: t20 Create Table: CREATE TABLE `t20` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(16) DEFAULT NULL, UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
清空表后 AUTO_INCREMENT=11 没有改变 自增长字段的值没有置为1
对于自增字段,在用delete删除后,再插入值,该字段仍然按照删除前的位置增长
从11开始
mysql> insert into t20(name) values('mike'); Query OK, 1 row affected (0.01 sec) mysql> select * from t20; +----+------+ | id | name | +----+------+ | 11 | mike | +----+------+ 1 row in set (0.00 sec)
清空表不要用delete清
delete 用在和where语句配合
delete from t20 where id=3;
用在删除固定的记录
用truncate
truncate是直接清空表,在删除大表时用它
mysql> show create table t20\G; *************************** 1. row *************************** Table: t20 Create Table: CREATE TABLE `t20` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(16) DEFAULT NULL, UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 1 row in set (0.00 sec) ERROR: No query specified mysql> truncate t20; Query OK, 0 rows affected (0.02 sec)
自增长字段没有了
mysql> show create table t20\G; *************************** 1. row *************************** Table: t20 Create Table: CREATE TABLE `t20` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(16) DEFAULT NULL, UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
再插入记录
mysql> insert into t20(name) values('jack'); Query OK, 1 row affected (0.00 sec) mysql> select * from t20; +----+------+ | id | name | +----+------+ | 1 | jack | +----+------+ 1 row in set (0.00 sec)
从1开始了
清空表:
delete from t1; #如果有自增id,新增的数据,仍然是以删除前的最后一样作为起始。
truncate table t1;数据量大,删除速度比上一条快,且直接从零开始,