内容详情 您现在的位置是: 首页> 其他随笔

MySQL自增长的bug?

发布时间:2021-07-13 18:38 已围观:961

摘要MySQL自增长的bug?

实验环境:

mysql> status
--------------
mysql Ver 14.14 Distrib 5.7.14, for Linux (x86_64) using EditLine wrapper

……

mysql> show variables like 'auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.03 sec)
auto_increment_offset:默认自增长起始点值;
auto_increment_increment:默认自增长增量值;

截图取材于标准MySQL5.7官方文档--没有盗版

mysql> create table t1(id int not null primary key auto_increment);
Query OK, 0 rows affected (0.21 sec)

mysql> insert into t1 values(null),(null),(null);
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t1;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.00 sec) 

 

1、修改自增长变量值

mysql> set @@auto_increment_offset=10;
Query OK, 0 rows affected (0.00 sec)

mysql> set @@auto_increment_increment=5;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 5     |
| auto_increment_offset    | 10    |
+--------------------------+-------+
2 rows in set (0.02 sec)

 通过变量的修改,现在的自增长起始值是10,增量是5

 

2、建表插值

mysql> create table t2(id int not null primary key auto_increment);
Query OK, 0 rows affected (0.11 sec)

mysql> insert into t2 values(null),(null),(null);
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

因为自增长起始值是10,增量是5;猜想结果值是10,15,20

 

3、查询结果

mysql> select * from t2;
+----+
| id |
+----+
|  4 |
|  9 |
| 10 |
+----+
3 rows in set (0.01 sec)

结果却非我们所猜想的???

继续插入值

mysql> insert into t2 values(null),(null),(null);
Query OK, 3 rows affected (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t2;
+----+
| id |
+----+
|  4 |
|  9 |
| 10 |
| 30 |
| 35 |
| 40 |
+----+
6 rows in set (0.00 sec)

经过多组测试,结果依旧(试验其他值都是正常的,唯独上述有问题)

那么,

  问题来了auto_increment_offset=10和auto_increment_increment=5这一组数值算是MySQL自增长里的一个bug吗

声明:本文内容摘自网络,版权归原作者所有。如有侵权,请联系处理,谢谢~
转发:GeaoZhang--https://www.cnblogs.com/geaozhang/p/6837546.html

赞一个 (82)