当前位置: 代码迷 >> MySQL >> mysql字段是自动增长部类结果却不是连续的
  详细解决方案

mysql字段是自动增长部类结果却不是连续的

热度:104   发布时间:2016-05-05 17:06:58.0
mysql字段是自动增长类型结果却不是连续的
      今天建了一张表,设置其"id"属性为自动增加类型,但是在查看所有数据的时候,却发现"id"的值不是连续增加的。建表的语法如下:
mysql> create table person_list    -> (    ->     id        int        auto_increment    unique,    ->    name    varchar(15)   not null,    ->    sex     varchar(15)   default "中",    ->    ID_C    varchar(20)   not null           unique,    -> vocation   varchar(8),    ->   salary   float         default 0.0    -> )charset=utf8;Query OK, 0 rows affected (0.09 sec)mysql> desc person_list;+----------+-------------+------+-----+---------+----------------+| Field    | Type        | Null | Key | Default | Extra          |+----------+-------------+------+-----+---------+----------------+| id       | int(11)     | NO   | PRI | NULL    | auto_increment || name     | varchar(15) | NO   |     | NULL    |                || sex      | varchar(15) | YES  |     | 中      |                || ID_C     | varchar(20) | NO   | UNI | NULL    |                || vocation | varchar(8)  | YES  |     | NULL    |                || salary   | float       | YES  |     | 0       |                |+----------+-------------+------+-----+---------+----------------+6 rows in set (0.00 sec)mysql>
之后我往这张表里面添加了一些数据,从中可以看出我所说的问题
mysql> select * from person_list;+----+-----------+------+--------------------+--------------+---------+| id | name      | sex  | ID_C               | vocation     | salary  |+----+-----------+------+--------------------+--------------+---------+|  1 | 诸葛亮    | 男   | 360428199908230544 | 蜀国         | 8654.56 ||  2 | 关羽      | 男   | 360428198908230544 | 蜀国         |  86.236 ||  3 | 貂蝉      | 女   | 361428198908231479 | 美女集团     | 4566.02 ||  4 | 西施      | 女   | 795410365478901234 | 美女集团     |   124.1 ||  5 | 雷军      | 男   | 469787036544025984 | 小米公司     |  1784.1 ||  6 | 乔布斯    | 男   | 456982103749651024 | 苹果公司     | 88888.9 ||  7 | 李白      | 中   | 147852036978410210 | NULL         |       0 || 10 | 乔布儿    | 男   | 456902103749651024 | 苹果公司     | 88888.9 || 13 | 乔布儿    | 男   | 056902103749651024 | 苹果公司     | 88888.9 |+----+-----------+------+--------------------+--------------+---------+9 rows in set (0.00 sec)mysql> 
如果我现在再插入一条记录,那么这个新增加的记录的"id"值会是14
mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);Query OK, 1 row affected (0.05 sec)mysql> select * from person_list;+----+-----------+------+--------------------+--------------+---------+| id | name      | sex  | ID_C               | vocation     | salary  |+----+-----------+------+--------------------+--------------+---------+|  1 | 诸葛亮    | 男   | 360428199908230544 | 蜀国         | 8654.56 ||  2 | 关羽      | 男   | 360428198908230544 | 蜀国         |  86.236 ||  3 | 貂蝉      | 女   | 361428198908231479 | 美女集团     | 4566.02 ||  4 | 西施      | 女   | 795410365478901234 | 美女集团     |   124.1 ||  5 | 雷军      | 男   | 469787036544025984 | 小米公司     |  1784.1 ||  6 | 乔布斯    | 男   | 456982103749651024 | 苹果公司     | 88888.9 ||  7 | 李白      | 中   | 147852036978410210 | NULL         |       0 || 10 | 乔布儿    | 男   | 456902103749651024 | 苹果公司     | 88888.9 || 13 | 乔布儿    | 男   | 056902103749651024 | 苹果公司     | 88888.9 || 14 | 马云      | 男   | 444989898784562304 | 淘宝公司     | 54588.9 |+----+-----------+------+--------------------+--------------+---------+10 rows in set (0.00 sec)mysql> 
然后我故意输入10条错误的语句(这个表的ID_C值不能重复)
mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);ERROR 1062 (23000): Duplicate entry '444989898784562304' for key 'ID_C'mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);ERROR 1062 (23000): Duplicate entry '444989898784562304' for key 'ID_C'mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);ERROR 1062 (23000): Duplicate entry '444989898784562304' for key 'ID_C'mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);ERROR 1062 (23000): Duplicate entry '444989898784562304' for key 'ID_C'mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);ERROR 1062 (23000): Duplicate entry '444989898784562304' for key 'ID_C'mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);ERROR 1062 (23000): Duplicate entry '444989898784562304' for key 'ID_C'mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);ERROR 1062 (23000): Duplicate entry '444989898784562304' for key 'ID_C'mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);ERROR 1062 (23000): Duplicate entry '444989898784562304' for key 'ID_C'mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);ERROR 1062 (23000): Duplicate entry '444989898784562304' for key 'ID_C'mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云","男","444989898784562304","淘宝公司",54588.85124);ERROR 1062 (23000): Duplicate entry '444989898784562304' for key 'ID_C'
之后我又增加1条正确的记录,结果发现该新记录的"ID"值是25,最后一条 跟 倒数第二条 的"ID"值刚好是11-1=10
mysql> insert into person_list (name,sex,ID_C,vocation,salary) value ("马云的儿子","男","444989898784562305","淘宝公司",54588.85124);Query OK, 1 row affected (0.04 sec)mysql> select * from person_list;+----+-----------------+------+--------------------+--------------+---------+| id | name            | sex  | ID_C               | vocation     | salary  |+----+-----------------+------+--------------------+--------------+---------+|  1 | 诸葛亮          | 男   | 360428199908230544 | 蜀国         | 8654.56 ||  2 | 关羽            | 男   | 360428198908230544 | 蜀国         |  86.236 ||  3 | 貂蝉            | 女   | 361428198908231479 | 美女集团     | 4566.02 ||  4 | 西施            | 女   | 795410365478901234 | 美女集团     |   124.1 ||  5 | 雷军            | 男   | 469787036544025984 | 小米公司     |  1784.1 ||  6 | 乔布斯          | 男   | 456982103749651024 | 苹果公司     | 88888.9 ||  7 | 李白            | 中   | 147852036978410210 | NULL         |       0 || 10 | 乔布儿          | 男   | 456902103749651024 | 苹果公司     | 88888.9 || 13 | 乔布儿          | 男   | 056902103749651024 | 苹果公司     | 88888.9 || 14 | 马云            | 男   | 444989898784562304 | 淘宝公司     | 54588.9 || 25 | 马云的儿子      | 男   | 444989898784562305 | 淘宝公司     | 54588.9 |+----+-----------------+------+--------------------+--------------+---------+11 rows in set (0.00 sec)mysql> 
所以我估摸着,在插入记录  语句错误的时候  自动增长类型的值会依旧增加,不是只有正确的时候才会+1。
  相关解决方案