zoukankan      html  css  js  c++  java
  • 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 ----
    -------------------------------------------------------------------------------------------------------
    版权全部,文章同意转载。但必须以链接方式注明源地址,否则追究法律责任!

  • 相关阅读:
    搭建Linux Kettle服务端重装系统软件或帮助
    JFrame加载Browser,Jxbrowser导入
    Bonita了解
    基于用户控件固定菜单MenuStrip/ToolStripMenuItem列表及动态菜单列表,是上一篇文章的综合和升级
    winfrom打开本地默认浏览器或打开IE浏览器
    Winfrom动态添加MenuStrip菜单,同时绑定反射事件,可扩展后期动态配置
    给GroupBox动态添加一个按钮,实现展开和折叠功能
    Rest导出Excel文件流
    List<HashMap>排序,List内存分页
    解决idea项目部署到Tomcat时Artiface没有文件
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7105215.html
Copyright © 2011-2022 走看看