• MySQL使用AUTO_INCREMENT列的表注意事项之delete数据篇


    1. 说明

    在对带有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


    2. MyISAM表


    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 t7G
    *************************** 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 t7G
    *************************** 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)
    


    3. InnoDB表


    MySQL [bosco]> show create table t2G
    *************************** 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 ----
    -------------------------------------------------------------------------------------------------------
    版权全部,文章同意转载。但必须以链接方式注明源地址,否则追究法律责任!

  • 相关阅读:
    五个字符就能让你电脑死机
    易语言e.exe在一些系统运行出错解决方法
    检测是否联网
    JS判断设备的类型
    JavaScript判断移动端及pc端访问不同的网站
    代码片段
    WEB前端知识在乱花渐欲迷人眼的当下,如何分清主次和学习优先级呢?
    说说JSON和JSONP,也许你会豁然开朗
    HTML5 LocalStorage 本地存储
    namenode 和datanode无法启动,错误:FSNamesystem initialization failed. datanode.DataNode: Incompatible namespaceIDs
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7105215.html
Copyright © 2020-2023  润新知