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

时间: 2024-08-03 05:20:45

MySQL auto_increment_increment,auto_increment_offset 用法的相关文章

MySQL Group By用法

MySQL Group By用法 我们现在回到函数上.记得我们用 SUM 这个指令来算出所有的 Sales (营业额)吧!如果我们的需求变成是要算出每一间店 (store_name) 的营业额 (sales),那怎么办呢?在这个情况下,我们要做到两件事:第一,我们对于 store_name 及 Sales 这两个栏位都要选出.第二,我们需要确认所有的 sales 都要依照各个 store_name 来分开算.这个语法为: SELECT "栏位1", SUM("栏位2"

mysql update select用法实例

  mysql update select用法实例 应该使用inner join,即: UPDATE friends INNER JOIN users ON friends.friendid=users.userid SET friends.friendname=users.username MySQL是通过临时表来实现FROM子句里面的嵌套查询,那么把嵌套查询装进另外一个嵌套查询里,可使FROM子句查询和保存都是在临时表里进行,然后间接地在外围查询被引用. 我们来看如下sql语句: updat

php中mysql操作buffer用法详解

 这篇文章主要介绍了php中mysql操作buffer用法,以实例形式较为详细的分析了mysql操作buffer的技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了php中mysql操作buffer用法.分享给大家供大家参考.具体分析如下: php与mysql的连接有三种方式,mysql,mysqli,pdo.不管使用哪种方式进行连接,都有使用buffer和不使用buffer的区别. 什么叫使用buffer和不使用buffer呢? 客户端与mysql服务端进行查询操作,查询

mysql group_concat()函数用法总结_Mysql

本文实例讲述了mysql group_concat()函数用法.分享给大家供大家参考,具体如下: group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果.比较抽象,难以理解. 通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来.要返回哪些列,由函数参数(就是字段名)决定.分组必须有个标准,就是根据group by指定的列进行分组. group_concat函数应该是在内部执行了group by语句,这

MySQL子查询用法实例分析_Mysql

本文实例讲述了MySQL子查询用法.分享给大家供大家参考,具体如下: 假设表my_tbl包含三个字段a,b,c:现在需要查询表中列a的每个不同值下的列b为最小值的记录量. 比如表记录为: a  b  c 1  3  'cd' 2  3  'nhd' 1  5  'bg' 2  6  'cds' 1  7  'kiy' 3  7  'vsd' 3  8  'ndf' 希望得到结果为: a  b  c 1  3  'cd' 2  3  'nhd' 3  7  'vsd' (1) 其中一个做法:先查出

MySQL切分查询用法分析_Mysql

本文实例讲述了MySQL切分查询用法.分享给大家供大家参考,具体如下: 对于大查询有时需要'分而治之',将大查询切分为小查询: 每个查询功能完全一样,但只完成原来的一小部分,每次查询只返回一小部分结果集. 删除旧的数据就是一个很好地例子.定期清理旧数据时,如果一条sql涉及了大量的数据时,可能会一次性锁住多个表或行,耗费了大量的系统资源,却阻塞了其他很多小的但重要的查询.将一个大得DELETE语句切分为较小的查询时,可以尽量减少影响msql的性能,同时减少mysql复制造成的延迟. 例如,每个月

MySQL解决抓取文章的html标签替换及其mysql函数的用法说明

刚刚做完了一个手机客户端的攻略的Html5 Web App页面,新的需求出现了:由于攻略文章是抓取过来的,有很多外链,一开始没有过滤.于是先用PHP写了一个过滤函数,然后批量执行更新相关数据库记录即可. public static function filter_newslink($aid){ $content = mod_news :: get_newscont($aid); //先过滤图片的外链 $content = preg_replace('/<a (.*)>(<img.*>

mysql中auto_increment用法详解

auto increment mysql的自增步长可以通过下面的命令查询, mysql> SHOW VARIABLES LIKE 'auto_inc%'; +--------------------------+-------+ | Variable_name            | Value | +--------------------------+-------+ | auto_increment_increment | 1     | | auto_increment_offset 

MySQL的LAST_INSERT_ID用法举例

环境:MySQL Sever 5.1 + MySQL命令行工具 首先看个例子(主键是自增长): mysql> insert into bankaccount(name,balance) values('123', 1000); Query OK, 1 row affected (0.06 sec) mysql> insert into bankstatement(action, txdate, amt, toaccno, fromaccno) values ('122', curdate(),