在對帶有AUTO_INCREMENT列的表delete掉所有數據時:
(1)對於MyISAM表,在delete表中所有數據時沒有任何風險,隨意折騰;
(2)對於InnoDB表,在delete表中所有數據時,是可能有風險的,可能會引入一個大坑,具體看後面的實驗。
環境描述:RHEL 6.4 x86_64 + MySQL 5.6.19
blog地址:http://blog.csdn.net/hw_libo/article/details/40149173
在維護有AUTO_INCREMENT列的表時,另外一個注意點,參考:
MySQL使用AUTO_INCREMENT列的表注意事項之delete數據篇
http://blog.csdn.net/hw_libo/article/details/40097125
MySQL [bosco]> CREATE TABLE `t7` ( -> `id` int(10) unsigned NOT NULL AUTO_INCREMENT, -> `name` varchar(20) NOT NULL DEFAULT '', -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM; Query OK, 0 rows affected (0.05 sec) MySQL [bosco]> insert into t7(name) values('GZ'),('SH'),('BJ'),('SZ'),('HZ'); Query OK, 5 rows affected (0.03 sec) Records: 5 Duplicates: 0 Warnings: 0 MySQL [bosco]> select * from t7; +----+------+ | id | name | +----+------+ | 1 | GZ | | 2 | SH | | 3 | BJ | | 4 | SZ | | 5 | HZ | +----+------+ 5 rows in set (0.00 sec) MySQL [bosco]> delete from t7; Query OK, 5 rows affected (0.03 sec) MySQL [bosco]> show create table t7\G *************************** 1. row *************************** Table: t7 Create Table: CREATE TABLE `t7` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 1 row in set (0.00 sec) MySQL [bosco]> insert into t7(name) values('NJ'); Query OK, 1 row affected (0.07 sec) MySQL [bosco]> select * from t7; +----+------+ | id | name | +----+------+ | 6 | NJ | +----+------+ 1 row in set (0.00 sec) 如果在delete from t2後,重啟了mysqld,AUTO_INCREMENT並不會被重置: MySQL [bosco]> show create table t7\G *************************** 1. row *************************** Table: t7 Create Table: CREATE TABLE `t7` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
MySQL [bosco]> show create table t2\G *************************** 1. row *************************** Table: t2 Create Table: CREATE TABLE `t2` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) MySQL [bosco]> insert into t2(name) values('GZ'),('SH'),('BJ'),('SZ'),('HZ'); Query OK, 5 rows affected (0.04 sec) Records: 5 Duplicates: 0 Warnings: 0 MySQL [bosco1]> select * from t2; +----+------+ | id | name | +----+------+ | 1 | GZ | | 2 | SH | | 3 | BJ | | 4 | SZ | | 5 | HZ | +----+------+ 5 rows in set (0.00 sec) MySQL [bosco]> delete from t2; ## 直接將t2表中的數據全部刪除,使用的是delete而非truncate Query OK, 5 rows affected (0.04 sec) MySQL [bosco]> insert into t2(name) values('NJ'); ## 再次插入數據時,自增id會自動為下一編號 Query OK, 1 row affected (0.02 sec) MySQL [bosco]> select * from t2; +----+------+ | id | name | +----+------+ | 6 | NJ | +----+------+ 1 row in set (0.00 sec) 但是,如果在delete from t2後,重啟了mysqld,那麼情況完全不一樣了,這個重啟會重置id。 MySQL [bosco]> insert into t2(name) values('FJ'); ## 再次插入數據時,自增id會被重置編號 Query OK, 1 row affected (0.02 sec) MySQL [bosco]> select * from t2; +----+------+ | id | name | +----+------+ | 1 | FJ | ## 再次插入新值時,id編號會重新從頭開始 +----+------+ 1 row in set (0.00 sec)
blog地址:http://blog.csdn.net/hw_libo/article/details/40149173
-- Bosco QQ:375612082
---- END ----
-------------------------------------------------------------------------------------------------------
版權所有,文章允許轉載,但必須以鏈接方式注明源地址,否則追究法律責任!