mysql auto increment offset_MySQL auto_increment_increment,auto_increment_offset 用法
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之后再按步长递增。
mysql auto increment offset_MySQL auto_increment_increment,auto_increment_offset 用法相关推荐
- mysql auto increment offset_mysql auto_increment_increment和auto_increment_offset
关于 mysql自增列的有2个参数: auto_increment_increment auto_increment_offset auto_increment_offset确定AUTO_INCREM ...
- mysql auto increment 插入_MySQL里AUTO_INCREMENT表里插入0值的问题
在使用MYSQL数据库时,无法设置AUTO_INCREMENT从0开始自增,之后查询了相关资料整理. 快速概览 AUTO_INCREMENT列满足条件 在不同数据库引擎下所具有的特征 解决AUTO_I ...
- MySQL事务 - 自增ID的回滚以及Auto Increment在InnoDB的实现
自增ID未回滚 首先做一个测试 CREATE TABLE auto_inc_test( id int auto_increment, test_id int, primary key id(id))E ...
- Mysql 自动递增(AUTO INCREMENT) 资料
AUTO INCREMENT 使用 检索最近自动生成AUTO_INCREMENT值 设置1以外的值开始 InnoDB AUTO_INCREMENT 计数器初始化 5.7版本及之前 8.0版本 查询表的 ...
- SQL AUTO INCREMENT 字段
SQL AUTO INCREMENT 字段 Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字 ...
- autoincrement mysql_SQL AUTO INCREMENT 字段 | 菜鸟教程
SQL AUTO INCREMENT 字段 Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字 ...
- 十七 SQL ALTER 与 AUTO INCREMENT
SQL ALTER TABLE 语句 ALTER TABLE 语句 ALTER TABLE 语句用于在已有的表中添加.删除或修改列. SQL ALTER TABLE 语法 如需在表中添加列,请使用下面 ...
- PostgreSQL AUTO INCREMENT
PostgreSQL AUTO INCREMENT(自动增长) AUTO INCREMENT(自动增长) 会在新记录插入表中时生成一个唯一的数字. PostgreSQL 使用序列来标识字段的自增长,数 ...
- PostgreSQL AUTO INCREMENT(自动增长)
PostgreSQL AUTO INCREMENT(自动增长) AUTO INCREMENT(自动增长) 会在新记录插入表中时生成一个唯一的数字. PostgreSQL 使用序列来标识字段的自增长,数 ...
最新文章
- ospf专题二:虚链路
- String判断为空的方式
- asn1 pem pfx格式证书_Springboot中详细配置SSL证书
- JavaEE6入门02—Myeclipse8.5+GlassFish
- 指定rviz的点启动_好消息!武汉已经启动新冠疫苗紧急接种工作
- 在 Rolling Update 中使用 Health Check - 每天5分钟玩转 Docker 容器技术(146)
- [maven] springboot将jar包打包到指定目录
- 深入理解java虚拟机--线程安全与锁优化
- 广播地址的作用_跟百哥学网络16:ARP地址解析协议分析
- 基于JavaScript实现网页计算器
- LwIP-Win32试用笔记
- java用cookie最新浏览商品_jQuery.cookie.js实现记录最近浏览过的商品功能示例
- CCF201812-5 管道清洁【最小费可行流】(100分题解链接)
- docker 镜像备份magento 2.2.3
- c语言调用树莓派usb摄像头,树莓派接多个USB摄像头,使用opencv打开指定的某一个摄像头...
- 四六级得分技巧备考和心态
- Python BeautifuSoup 库 mooc 中国大学学习
- SIM卡类型之间的差异-选择哪种SIM卡
- ip地址映射-方便开发微信公众号,小程序等
- c语言求幸运数字程序,算法题挑选幸运数字,该如何处理
热门文章
- vscode常用插件 - Path Autocomplete
- 哪些原因会导致头晕头痛,日常应该如何去注意呢?
- 用QT做串口通讯,读取身份证信息
- c语言圆环内外半径圆环面积,《圆环的面积》微课教学设计
- PNG字幕文件自动生成工具
- 数据标准化方法z-score讲解(matlab)
- 学会这几个软件,你就可以获得次时代建模师称号!
- 漫画 | Code Review快把我逼疯了!
- 【论文笔记】(VLDB 2020) A Benchmarking Study of Embedding-based Entity Alignment for Knowledge
- 计蒜客 人人都有极客精神(模拟)