MySQL auto_increment_increment,auto_increment_offset 用法

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:     MySQL中对于表上ID自增列可以在创建表的时候来指定列上的auto_increment属性;等同于SQL server中的identity属性;Oracle则是通过Sequence方式来实现。

    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之后再按步长递增。

鹏城DBA总群

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1天前
|
存储 关系型数据库 MySQL
MySQL各字符集、排序规则的由来、用法,区别和联系
MySQL支持多种字符集和排序规则,这些在数据库设计和数据处理中起着重要作用。下面是它们的由来、用法、区别和联系: 1. **字符集(Character Set)**: - **由来**:字符集定义了数据库中可以存储的字符集合,以及这些字符在数据库中的存储方式。 - **用法**:在创建数据库或表时,可以指定所需的字符集。常见的字符集包括UTF-8、UTF-16、Latin1等。 - **区别和联系**:不同的字符集支持不同的字符范围和存储方式,选择合适的字符集可以确保数据的正确存储和处理。例如,UTF-8支持全球范围内的大多数字符,而Latin1只支持西欧语言字符集。
|
2天前
|
自然语言处理 关系型数据库 MySQL
一文明白MySQL索引的用法及好处
一文明白MySQL索引的用法及好处
20 0
|
2天前
|
关系型数据库 MySQL
MySQL union和union all的用法详解和区别
MySQL union和union all的用法详解和区别
11 0
|
2天前
|
SQL 关系型数据库 MySQL
MySQL 基本概念 基础用法 增删改查(特殊查询)语法 详细篇
MySQL 基本概念 基础用法 增删改查(特殊查询)语法 详细篇
|
5月前
|
SQL 关系型数据库 MySQL
MySQL 中exists与in及any的用法详解
MySQL 中exists与in及any的用法详解
60 3
|
2天前
|
关系型数据库 MySQL
MySQL中CONCAT() ,CONCAT_WS() ,GROUP_CONCAT()的用法
MySQL中CONCAT() ,CONCAT_WS() ,GROUP_CONCAT()的用法
16 2
|
2天前
|
SQL 关系型数据库 MySQL
mysql结果垂直显示-\g和\G的用法
mysql结果垂直显示-\g和\G的用法
46 0
|
2天前
|
存储 安全 关系型数据库
MySQL 临时表的用法和特性
MySQL 临时表的用法和特性
|
7月前
|
关系型数据库 MySQL Unix
【MySQL用法】MySQL 中 datetime 和 timestamp 的区别与选择
【MySQL用法】MySQL 中 datetime 和 timestamp 的区别与选择
138 0
|
2天前
|
SQL 关系型数据库 MySQL
Mysql SQL的一些特殊用法记录
1、查询group by having 中having不起作用,及解决
16 0

推荐镜像

更多