數據庫查詢中,涉及auto_increment中的參數變量一共有兩個
[root@localhost][(none)]> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
rows in set (0.00 sec)
auto_increment_increment 自增長值
auto_increment_offset 漂移值 也就是步長
由於auto_increment_increment屬於全局可變的變量,故此可以通過修改自增值來達到測試的目的
[root@localhost][(none)]> create table boss.autoinc1(col int not null auto_increment primary key);
Query OK, 0 rows affected (1.03 sec)
[root@localhost][(none)]> set @@auto_increment_increment=10;
Query OK, 0 rows affected (0.00 sec)
[root@localhost][(none)]> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 10 |
| auto_increment_offset | 1 |
+--------------------------+-------+
rows in set (0.00 sec)
從上面可以看到,自增從10開始,那麼此時插入數據會是什麼結果?
[root@localhost][(none)]> insert into boss.autoinc1 values(null),(null),(null),(null);
Query OK, 4 rows affected (0.29 sec)
Records: 4 Duplicates: 0 Warnings: 0
[root@localhost][(none)]> select col from boss.autoinc1;
+-----+
| col |
+-----+
| 1 |
| 11 |
| 21 |
| 31 |
+-----+
rows in set (0.00 sec)
從結果集來看,auto_incremetn_increment的自增,爲下一個跟上一個的間隔爲10,也就是11->21->31->41依次類推
此時,我們設置offset這個的偏移值,那麼數據則會
[root@localhost][(none)]> create table boss.autoinc2(col int not null auto_increment primary key);
Query OK, 0 rows affected (1.31 sec)
[root@localhost][(none)]> insert into boss.autoinc2 values(null),(null),(null),(null);
Query OK, 4 rows affected (0.14 sec)
Records: 4 Duplicates: 0 Warnings: 0
[root@localhost][(none)]> select col from boss.autoinc2;
+-----+
| col |
+-----+
| 5 |
| 15 |
| 25 |
| 35 |
+-----+
rows in set (0.00 sec)
可以看到,第一個是從基數1偏移到5個值(1、2、3、4、5)然後自動增值,每次進10這麼處理
本質的邏輯爲auto_increment_offset + N × auto_increment_increment N表示第幾次,從0的技術開始計算
比如5+010,5+110 即
[root@localhost][mysql]> set @@auto_increment_offset=5;
Query OK, 0 rows affected (0.00 sec)
[root@localhost][mysql]> create table boss.autoinc6(col int not null auto_increment primary key);
Query OK, 0 rows affected (0.36 sec)
[root@localhost][mysql]> set @@auto_increment_increment=10;
Query OK, 0 rows affected (0.00 sec)
[root@localhost][mysql]> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 10 |
| auto_increment_offset | 5 |
+--------------------------+-------+
rows in set (0.00 sec)
[root@localhost][mysql]> insert into boss.autoinc6 values(null),(null),(null),(null);
Query OK, 4 rows affected (0.08 sec)
Records: 4 Duplicates: 0 Warnings: 0
[root@localhost][mysql]> select col from boss.autoinc6;
+-----+
| col |
+-----+
| 5 |
| 15 |
| 25 |
| 35 |
+-----+
rows in set (0.00 sec)
轉載自
https://www.cnblogs.com/olinux/p/6518766.html