设为首页 收藏本站
查看: 479|回复: 0

[经验分享] MySQL auto_increment_increment,auto_increment_offset 用法

[复制链接]

尚未签到

发表于 2016-10-18 08:14:38 | 显示全部楼层 |阅读模式
   MySQL中对于表上ID自增列可以在创建表的时候来指定列上的auto_increment属性;等同于SQL server中的identity属性;Oracle则是通过Sequence方式来实现。在MySQL中,系统变量auto_increment_increment,auto_increment_offset 影响自增列的值及其变化规则。本文主要描述这两个系统变量的相关用法。
  
  1、auto_increment_increment与auto_increment_offset作用


auto_increment_increment控制列中的值的增量值,也就是步长。
auto_increment_offset确定AUTO_INCREMENT列值的起点,也就是初始值。
变量范围:可以在全局以及session级别设置这2个变量
--当前系统环境
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     |
+--------------------------+-------+

  2、演示auto_increment_increment与auto_increment_offset


--创建演示表,使用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     |
+--------------------------+-------+
--清空表t1
root@localhost[tempdb]> truncate table t1;
--再次插入记录
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 |
+----+-------+

  3、auto_increment_increment与auto_increment_offset取值范围

--将变量auto_increment_increment设置为0
root@localhost[tempdb]> set session auto_increment_increment=0;
--实际值变成了1
root@localhost[tempdb]> show variables like '%auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 5     |
+--------------------------+-------+
--同样将auto_increment_offset设置为0
root@localhost[tempdb]> set session auto_increment_offset=0;
--实际值也变成了1
root@localhost[tempdb]> show variables like '%auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
--下面尝试将2个变量设置为大于65535
root@localhost[tempdb]> set session auto_increment_increment=65537;
root@localhost[tempdb]> set session auto_increment_offset=65537;
--其实际的值都变成了65535
root@localhost[tempdb]> show variables like '%auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 65535 |
| auto_increment_offset    | 65535 |
+--------------------------+-------+
--尝试为2个变量设置为负值
root@localhost[tempdb]> set session auto_increment_offset=-2;
root@localhost[tempdb]> set session auto_increment_increment=-5;
--下面的查询可以看出全部恢复到缺省值1
root@localhost[tempdb]> show variables like '%auto_increment%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
由上可以看出2个变量只能设置为1至65535之间的整数值。
所有非正整数全部会置为缺省值1,大于65535的值会被自动置为65535。

  4、全局与session级别的设置

--查看全局范围这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     |
+--------------------------+-------+
--Author: Leshami
--Blog  : http://blog.csdn.net/leshami
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 |
+----+-------+
New_value = auto_increment_offset+ N * auto_increment_increment
New_value1 = 1 + 1 * 5 = 6
New_value2 = 1 + 2 * 5 = 11
--下面是修改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   |
+----+-------+
这个id为22,应该是这样推算来的:max(id)+(new_offset-old_offset)+increment
也就是说变化auto_increment_offset后的第一个值为max(id)+(new_offset-old_offset)+increment之后再按步长递增。

DSC0000.png

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-287681-1-1.html 上篇帖子: 比较两个mysql数据库表结构的差异 下篇帖子: MySQL记录存在则更新,不存在则插入
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表