--当前系统环境
root@localhost[(none)]> show variables like 'version';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5.5.39-log |
+---------------+------------+
root@localhost[mysql]> create database tempdb;
root@localhost[mysql]> use tempdb;
--查看变量auto_increment_increment与auto_increment_offset
root@localhost[tempdb]> show variables like '%auto_incre%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
--创建演示表,使用auto_increment子句
root@localhost[tempdb]> create table t1(id int not null auto_increment primary key, col varchar(20));
--插入记录
root@localhost[tempdb]> insert into t1(col) values('robin'),('fred'),('jack'),('james');
--下面可以看到id列起始值为1,增量为1
root@localhost[tempdb]> select * from t1;
+----+-------+
| id | col |
+----+-------+
| 1 | robin |
| 2 | fred |
| 3 | jack |
| 4 | james |
+----+-------+
--设置步长为5
root@localhost[tempdb]> set session auto_increment_increment=5;
root@localhost[tempdb]> show variables like '%auto_incre%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 5 |
| auto_increment_offset | 1 |
+--------------------------+-------+
--再次插入记录
root@localhost[tempdb]> insert into t1(col) values('robin'),('fred'),('jack'),('james');
--如下查询可以看到步长以5位基数发生变化
root@localhost[tempdb]> select * from t1;
+----+-------+
| id | col |
+----+-------+
| 1 | robin |
| 6 | fred |
| 11 | jack |
| 16 | james |
+----+-------+
--设置初始值为5
root@localhost[tempdb]> set session auto_increment_offset=5;
root@localhost[tempdb]> show variables like '%auto_incre%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 5 |
| auto_increment_offset | 5 |
+--------------------------+-------+
root@localhost[tempdb]> truncate table t1;
root@localhost[tempdb]> insert into t1(col) values('robin'),('fred'),('jack'),('james');
--下面是新的结果
root@localhost[tempdb]> select * from t1;
+----+-------+
| id | col |
+----+-------+
| 5 | robin |
| 10 | fred |
| 15 | jack |
| 20 | james |
+----+-------+
--查看全局范围这2个变量的值
root@localhost[tempdb]> show global variables like '%auto_increment%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
--下面分别设置session基本的值
root@localhost[tempdb]> set session auto_increment_increment=5;
root@localhost[tempdb]> set session auto_increment_offset=10;
--查看session级别的值
root@localhost[tempdb]> show session variables like '%auto_increment%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 5 |
| auto_increment_offset | 10 |
+--------------------------+-------+
--查看全局级别的值
root@localhost[tempdb]> show global variables like '%auto_increment%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
--设置全局级别的值
root@localhost[tempdb]> set global auto_increment_increment=2;
root@localhost[tempdb]> set global auto_increment_offset=3;
root@localhost[tempdb]> show global variables like '%auto_increment%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 2 |
| auto_increment_offset | 3 |
+--------------------------+-------+
5、已有auto_increment列值任一变量变化的情形
root@localhost[tempdb]> truncate table t1;
root@localhost[tempdb]> show variables like '%auto_increment%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
root@localhost[tempdb]> insert into t1(col) values('robin'),('fred'),('jack');
root@localhost[tempdb]> select * from t1;
+----+-------+
| id | col |
+----+-------+
| 1 | robin |
| 2 | fred |
| 3 | jack |
+----+-------+
root@localhost[tempdb]> set session auto_increment_increment=5;
root@localhost[tempdb]> show variables like '%auto_increment%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 5 |
| auto_increment_offset | 1 |
+--------------------------+-------+
root@localhost[tempdb]> insert into t1(col) values('david'),('tim'),('jerry');
root@localhost[tempdb]> select * from t1;
+----+-------+
| id | col |
+----+-------+
| 1 | robin |
| 2 | fred |
| 3 | jack |
| 6 | david |
| 11 | tim |
| 16 | jerry |
+----+-------+
--下面是修改auto_increment_offset后的结果
root@localhost[tempdb]> set session auto_increment_offset=2;
root@localhost[tempdb]> insert into t1(col) values('lewis'),('ian');
root@localhost[tempdb]> select * from t1;
+----+-------+
| id | col |
+----+-------+
| 1 | robin |
| 2 | fred |
| 3 | jack |
| 6 | david |
| 11 | tim |
| 16 | jerry |
| 22 | lewis |
| 27 | ian |
+----+-------+