MySQL系统变量auto_increment_increment与auto_increment_offset学习总结

在MySQL中,系统变量auto_increment_increment与auto_increment_offset是与自增列相关的两个参数变量。在官方文档中,将其划分为Replication Master Options and Variables 。具体参考官方文档1...

MySQL中,系统变量auto_increment_incrementauto_increment_offset是与自增列相关的两个参数变量。在官方文档中,将其划分为Replication Master Options and Variables 。具体参考官方文档17.1.6.2 Replication Master Options and Variables

 

    auto_increment_offset     AUTO_INCREMENT列值的起点,也就是初始值。取值范围是1 .. 65535

   auto_increment_increment   控制列中的值的增量值,也就是步长。其默认值是1,取值范围是1 .. 65535

 

系统变量auto_increment_incrementauto_increment_offset 都有会话级别和全局级别两个值(注意:设置全局系统变量时,对当前连接或已存在的连接不生效,只对新的连接有效)。它们的取值范围为1 ..65535, 如果设置的时候超过这个范围的话,会是什么情况? 如下所示:

 

mysql> show variables like 'auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
4 rows in set (0.00 sec)
 
mysql> set session auto_increment_increment=0;
Query OK, 0 rows affected, 1 warning (0.01 sec)
 
mysql> show variables like 'auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql>  set session auto_increment_increment=65536;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql>  show variables like 'auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 65535 |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql> 
mysql> set session auto_increment_offset=-1;
Query OK, 0 rows affected, 1 warning (0.04 sec)
 
mysql> show variables like 'auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 65535 |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)

 

clip_image001

 

如上所示,如果系统变量设置其值小于10或负数),MySQL会默认设置为1 ,如果大于65535MySQL会默认设置为65535. 也就是说这两个系统变量的取值范围为 1 ... 65535.

 

 

系统变量auto_increment_increment修改后,自增列的变化规律

 

 

如果我们想知道系统变量auto_increment_increment变化后,自增列的变化规律,最简单、有效的方式就是实验测试,如下所示:

 

mysql> drop table if exists test;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> create table test(id int auto_increment primary key, name varchar(32));
Query OK, 0 rows affected (0.03 sec)
 
mysql> insert into test(name) value('kerry1');
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into test(name) value('kerry2');
Query OK, 1 row affected (0.01 sec)
 
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | kerry1 |
|  2 | kerry2 |
+----+--------+
2 rows in set (0.01 sec)
 
mysql> show  variables like 'auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql> set session auto_increment_increment=3;
Query OK, 0 rows affected (0.00 sec)
 
mysql> insert into test(name) value('kerry3');
Query OK, 1 row affected (0.01 sec)
 
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | kerry1 |
|  2 | kerry2 |
|  4 | kerry3 |
+----+--------+
3 rows in set (0.00 sec)
 
mysql> 

 

clip_image002

 

mysql>  set session auto_increment_increment=1;
Query OK, 0 rows affected (0.00 sec)
 
mysql> show  variables like 'auto_increment%';
+--------------------------+-------+
                        
  • 发表于 2019-07-09 11:00
  • 阅读 ( 270 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除