MySQL 中有关auto_increment及auto_increment_offset方面的介绍
2024-08-29 04:35:44
数据库查询中,涉及到auto_increment中的参数变量一共有两个
[root@localhost][(none)]> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
2 rows in set (0.00 sec)
auto_increment_increment:自增值
auto_increment_offset:漂移值,也就是步长 由于auto_increment_increment 属于全局可变的变量,故此可以通过修改自增值来达到测试目的
[root@localhost][(none)]> create table boss.autoinc1(col int not null auto_increment primary key);
Query OK, 0 rows affected (1.03 sec) [root@localhost][(none)]> set @@auto_increment_increment=10;
Query OK, 0 rows affected (0.00 sec) [root@localhost][(none)]> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 10 |
| auto_increment_offset | 1 |
+--------------------------+-------+
2 rows in set (0.00 sec)
从上面可以看到,自增从10开始,那么此时插入数据会是什么结果?
[root@localhost][(none)]> insert into boss.autoinc1 values(null),(null),(null),(null);
Query OK, 4 rows affected (0.29 sec)
Records: 4 Duplicates: 0 Warnings: 0 [root@localhost][(none)]> select col from boss.autoinc1;
+-----+
| col |
+-----+
| 1 |
| 11 |
| 21 |
| 31 |
+-----+
4 rows in set (0.00 sec)
从结果集来看,auto_increment_increment的自增,为下一个跟上一个的间隔为10,也就是11->21->31->41以此类推
此时,我们设置offset这个的偏移值,那么数据则会
[root@localhost][(none)]> create table boss.autoinc2(col int not null auto_increment primary key);
Query OK, 0 rows affected (1.31 sec) [root@localhost][(none)]> insert into boss.autoinc2 values(null),(null),(null),(null);
Query OK, 4 rows affected (0.14 sec)
Records: 4 Duplicates: 0 Warnings: 0 [root@localhost][(none)]> select col from boss.autoinc2;
+-----+
| col |
+-----+
| 5 |
| 15 |
| 25 |
| 35 |
+-----+
4 rows in set (0.00 sec)
可以看到,第一个是从基数1偏移到5个值(1,2,3,4,5),然后自动增值,每次进10这么处理
本质的逻辑为 auto_increment_offset + N × auto_increment_increment N表示第几次,从0的技术开始计算
比如5+0*10,5+1*10,即
[root@localhost][mysql]> set @@auto_increment_offset=5;
Query OK, 0 rows affected (0.00 sec) [root@localhost][mysql]> create table boss.autoinc6(col int not null auto_increment primary key);
Query OK, 0 rows affected (0.36 sec) [root@localhost][mysql]> set @@auto_increment_increment=10;
Query OK, 0 rows affected (0.00 sec) [root@localhost][mysql]> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 10 |
| auto_increment_offset | 5 |
+--------------------------+-------+
2 rows in set (0.00 sec) [root@localhost][mysql]> insert into boss.autoinc6 values(null),(null),(null),(null);
Query OK, 4 rows affected (0.08 sec)
Records: 4 Duplicates: 0 Warnings: 0 [root@localhost][mysql]> select col from boss.autoinc6;
+-----+
| col |
+-----+
| 5 |
| 15 |
| 25 |
| 35 |
+-----+
4 rows in set (0.00 sec)
最新文章
- help man info 三个的区别
- WPF Adorner+附加属性 实现控件友好提示
- CentOS VMware 配置IP小结 静态 配置 桥接 NAT
- 关于mysql(或MariaDB)中的用户账号格式
- Linux下中文显示乱码问题
- UVa 1584 Circular Sequence --- 水题
- 三分钟读懂Oracle数据库容灾架之DataGuard
- java MongoDB driver error infos
- 获取Exe文件版本信息的函数(使用GetFileVersionInfo得到TFileVersionInfo结构体,包含12项内容)
- 解决ScrollView中的ListView无法显示全
- Java简单工厂模式以及来自lambda的优化
- 分布式事务2PC_PENDING异常处理
- Python多进程并发(multiprocessing)
- 面向对象【day07】:知识点回顾(十一)
- Go 开源博客平台 Pipe 1.0.0 发布!
- IOS初级:NSKeyedArchiver
- Centos 二进制安装node.js
- 工作流JBPM_day01:3-使用JBPM的API添加与执行流程
- day4 基础
- 【招聘需求】前端开发/PHP工程师【往全栈发展】
热门文章
- mac系统中搭建apache+mysql+php的开发环境,安装mysql后,登录报错:mac ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- URL.createObjectURL()
- git服务器gitlab之搭建和使用
- 信号处理的好书Digital Signal Processing - A Practical Guide for Engineers and Scientists
- sql语句优化技巧
- Triangulation by Ear Clipping(耳切法处理多边形三角划分)
- linu保持远程会话
- Codeforces 460E Roland and Rose(暴力)
- AAC终结者Opus音频编码器的瑞士军刀,编译android ios
- python 面试题 string int