MySQL 中有关auto_increment及auto_increment_offset方面的介绍
数据库查询中,涉及到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)
MySQL 中有关auto_increment及auto_increment_offset方面的介绍的更多相关文章
- 更改mysql中当前auto_increment的值的方法
最近给自己网站更改mysql中当前auto_increment的值 如果在mysql中一个表test中的ID字段设为auto_increment插入两条记录后ID=2,这时删除1条记录,再插入一条变成 ...
- Mysql中的auto_increment
Mysql中的auto_increment 1.创建 2.使用 [1]如果不写固定列,则必须要插入该列,可以直接写Null,否则会报错 [2]可以直接在auto_increment 列上直接插入显式值 ...
- mysql中模糊查询的四种用法介绍
下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...
- 修改mysql中的auto_increment
在mysql数据库中,如何修改自增值auto_increment呢?请看下面的语句: 1.sql语句 ALTER TABLE table_name AUTO_INCREMENT=1 2截断表,trun ...
- MySQL中四种常用存储引擎的介绍
MySQL常用的四种引擎的介绍 (1):MyISAM存储引擎: 不支持事务.也不支持外键,优势是访问速度快,对事务完整性没有 要求或者以select,insert为主的应用基本上可以用这个引擎来创建表 ...
- MySQL中auto_increment的基本特性
创建数据表时,经常会出现auto_increment这个词,下面就来了解一下它吧. MySQL的中AUTO_INCREMENT类型的属性用于为一个表中记录自动生成ID功能,可在一定程度上代替Oracl ...
- Mysql中自增字段(AUTO_INCREMENT)的一些常识
Mysql中自增字段(AUTO_INCREMENT)的一些常识: http://chengxuyuan.naxieshir.com/fenlei/2/p/151.html
- mysql学习笔记(二:中的auto_increment 理解
1.auto_increment 理解1 auto_increment是用于主键自动增长的,从1开始增长,当你把第一条记录删除时,再插入第二跳数据时,主键值是2,不是1. 例如: create tab ...
- MySQL中的数据类型以及完整性约束
数据类型 数据库mysql中也是分很多数据类型的,最常用的就是:数字类型.字符类型.日期类型.枚举与集合类型 一.数字类型: 默认都是有符号的,即正负号,若想无符号,在创建表时加unsigned.指定 ...
随机推荐
- (四)EasyUI 使用——form表单2 & window窗口
form表单组件主要有以下内容(如下图) 6. progressbar进度条 每隔1秒让进度条按随机数填充,直至充满进度条刻度(只能执行一次) 进度条: <div id=" ...
- PHP-CURL在POST请求时的注意事项
今天搞12306抢票, 在用CURL模拟POST请求校验验证码时, 无论如何12306都返回零, 正常应该返回True或者False, 最后查找原因如下 只是请求头 Content-Type 用了 a ...
- Ant 编译项目资源不足
http://www.cnblogs.com/interboy/archive/2008/07/15/1243265.html今天用ant编译项目出现 [javac] 系统资源不足.的错误,如下 Bu ...
- 牛散NO.1:MACD计啜诱多,勾魂枪连环夺命时
上证日线“连环夺命勾魂枪” 话说MACD中圈C的回勾,好事者皆认为新的冲击波即将曙光再现.伴随着K线出现红柱中阳,更多的投资者将会被这一勾诱惑得群情亢奋,盲断行情又要 起来了.但往往事与愿违,“潘金莲 ...
- POJ 3667 Hotel(线段树)
POJ 3667 Hotel 题目链接 题意:有n个房间,如今有两个操作 1.找到连续长度a的空房间.入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0 2.清空[a, a + b - 1 ...
- CSS3 :nth-child()伪类选择器
CSS3 :nth-child()伪类选择器 CSS3的强大,让人惊叹,人们在惊喜之余,又不得不为其艰难的道路感到可惜:好 的标准只有得到行业浏览器的良好支持才算得上“标准”.CSS3标 准已提出数年 ...
- ../lib//libscsdblog.so: undefined reference to `pthread_atfork'
代码中遇到这个问题,但是在makefile中已经添加了-lpthread. 最后发现问题时,引入库的顺序,把-lpthread放在最后就可以了.
- ArrayList和Vector性能对比
测试条件: 循环次数:1千万次 元素个数:1000个 测试结果: 总结:ArrayList获取元素非常快,不过添加元素没有Vector快,两者各有优势,Vector是线程安全的,而ArrayList是 ...
- row format delimited fields terminated by ','
row format delimited fields terminated by ',' 以','结尾的行格式分隔字段
- sublime Text 3 使用插件追踪函数
一.下载工具 https://pan.baidu.com/s/1R0bZMMGQeKTTajIA-9DU3w 或者 https://pan.baidu.com/s/1R0bZMMGQeKTTajIA- ...