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个变量

--当前系统环境
[email protected][(none)]> show variables like ‘version‘;
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.5.39-log |
+---------------+------------+

[email protected][mysql]> create database tempdb;

[email protected][mysql]> use tempdb;

--查看变量auto_increment_increment与auto_increment_offset
[email protected][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子句
[email protected][tempdb]> create table t1(id int not null auto_increment primary key, col varchar(20));

--插入记录
[email protected][tempdb]> insert into t1(col) values(‘robin‘),(‘fred‘),(‘jack‘),(‘james‘);

--下面可以看到id列起始值为1,增量为1
[email protected][tempdb]> select * from t1;
+----+-------+
| id | col   |
+----+-------+
|  1 | robin |
|  2 | fred  |
|  3 | jack  |
|  4 | james |
+----+-------+

--设置步长为5
[email protected][tempdb]> set session auto_increment_increment=5;

[email protected][tempdb]> show variables like ‘%auto_incre%‘;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 5     |
| auto_increment_offset    | 1     |
+--------------------------+-------+

--清空表t1
[email protected][tempdb]> truncate table t1;

--再次插入记录
[email protected][tempdb]> insert into t1(col) values(‘robin‘),(‘fred‘),(‘jack‘),(‘james‘);

--如下查询可以看到步长以5位基数发生变化
[email protected][tempdb]> select * from t1;
+----+-------+
| id | col   |
+----+-------+
|  1 | robin |
|  6 | fred  |
| 11 | jack  |
| 16 | james |
+----+-------+

--设置初始值为5
[email protected][tempdb]> set session auto_increment_offset=5;

[email protected][tempdb]> show variables like ‘%auto_incre%‘;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 5     |
| auto_increment_offset    | 5     |
+--------------------------+-------+

[email protected][tempdb]> truncate table t1;

[email protected][tempdb]> insert into t1(col) values(‘robin‘),(‘fred‘),(‘jack‘),(‘james‘);

--下面是新的结果
[email protected][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
[email protected][tempdb]> set session auto_increment_increment=0;

--实际值变成了1
[email protected][tempdb]> show variables like ‘%auto_increment%‘;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 5     |
+--------------------------+-------+

--同样将auto_increment_offset设置为0
[email protected][tempdb]> set session auto_increment_offset=0;

--实际值也变成了1
[email protected][tempdb]> show variables like ‘%auto_increment%‘;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+

--下面尝试将2个变量设置为大于65535
[email protected][tempdb]> set session auto_increment_increment=65537;

[email protected][tempdb]> set session auto_increment_offset=65537;

--其实际的值都变成了65535
[email protected][tempdb]> show variables like ‘%auto_increment%‘;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 65535 |
| auto_increment_offset    | 65535 |
+--------------------------+-------+

--尝试为2个变量设置为负值
[email protected][tempdb]> set session auto_increment_offset=-2;

[email protected][tempdb]> set session auto_increment_increment=-5;

--下面的查询可以看出全部恢复到缺省值1
[email protected][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个变量的值
[email protected][tempdb]> show global variables like ‘%auto_increment%‘;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+

--下面分别设置session基本的值
[email protected][tempdb]> set session auto_increment_increment=5;

[email protected][tempdb]> set session auto_increment_offset=10;

--查看session级别的值
[email protected][tempdb]> show session variables like ‘%auto_increment%‘;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 5     |
| auto_increment_offset    | 10    |
+--------------------------+-------+

--查看全局级别的值
[email protected][tempdb]> show global variables like ‘%auto_increment%‘;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+

--设置全局级别的值
[email protected][tempdb]> set global auto_increment_increment=2;

[email protected][tempdb]> set global auto_increment_offset=3;

[email protected][tempdb]> show global variables like ‘%auto_increment%‘;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 2     |
| auto_increment_offset    | 3     |
+--------------------------+-------+

5、已有auto_increment列值任一变量变化的情形

[email protected][tempdb]> truncate table t1;

[email protected][tempdb]> show variables like ‘%auto_increment%‘;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+

[email protected][tempdb]> insert into t1(col) values(‘robin‘),(‘fred‘),(‘jack‘);          

[email protected][tempdb]> select * from t1;
+----+-------+
| id | col   |
+----+-------+
|  1 | robin |
|  2 | fred  |
|  3 | jack  |
+----+-------+

[email protected][tempdb]> set session auto_increment_increment=5;

[email protected][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

[email protected][tempdb]> insert into t1(col) values(‘david‘),(‘tim‘),(‘jerry‘);

[email protected][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后的结果
[email protected][tempdb]> set session auto_increment_offset=2;

[email protected][tempdb]> insert into t1(col) values(‘lewis‘),(‘ian‘);

[email protected][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-07-31 11:03:05

MySQL auto_increment_increment,auto_increment_offset 用法的相关文章

mysql常用命令用法

1.创建数据库:create database database_name; 2.选择数据库:use database_name; 3.创建表:create table tablename(column1 data_type1, column2 data_type2,...,columnn datatypen); 4.设置表主键:alter table tablename add primary key(column_name); 5:修改表的字段名:alter table tablename

mysql not in用法

select * from zan where uid not in(select uid from zan where zhongjiang !=0) group by uid order by rand() limit 40 不过这个执行效率比较低,正在找更好用的方法 我觉得还是不如两条语句分开来写,先查出所有的uid,然后再用not in  这样查询速度快很多 $sql="select uid from zan where zhongjiang !='0'";$res=$dbs-

mysql 数据类型TIMESTAMP用法

在mysql数据库中,timestamp数据类型是一个比较特殊的数据类型,可以自动在不使用程序更新情况下只要更新了记录timestamp会自动更新时间. 通常表中会有一个Create date 创建日期的字段,其它数据库均有默认值的选项.MySQL也有默认值timestamp,但在MySQL中,不仅是插入就算是修改也会更新timestamp的值!这样一来,就不是创建日期了,当作更新日期来使用比较好!因此在MySQL中要记录创建日期还得使用datetime 然后使用NOW() 函数完成!1: 如果

MYSQL中LIMIT用法

MYSQL中LIMIT用法 SELECT * FROM tableName LIMIT [offset,] rows; 1.select * from table limit m,n(显示条数) 其中m是指记录开始的索引index(索引是从0开始的表示第一条记录 ) n是指从第m+1条开始,取n条. 结果是检索记录第m+1行至(m+n)行记录,共取出n条记录 ex: SELECT * FROM 表名 limit 6,5; 结果:检索记录第7行至11行记录,共取出5条记录. 2.n可以被设置为-1

mysql group by 用法解析(详细)

在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值.其原因是 distinct只能返回它的目标字段,而无法返回其它字段,这个问题让我困扰了很久,用distinct不能解决的话,我只有用二重循环查询来解决,而 这样对于一个数据量非常大的站来说,无疑是会直接影响到效率的.所以我花了很多时间来研究这个问题,网上也查不到解决方案 下面先来看看例子:

MySQL replace into 用法详细介绍

MySQL replace into 用法(insert into 的增强版) 在向表中插入数据的时候,经常遇到这样的情况:1. 首先判断数据是否存在: 2. 如果不存在,则插入:3.如果存在,则更新. 在 SQL Server 中可以这样处理: if not exists (select 1 from t where id = 1) insert into t(id, update_time) values(1, getdate()) else update t set update_time

mysql数据类型和用法

``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patience, the rules for which are as follows: Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbour on t

mysql的基本用法

创建数据库:create database [if not exist]name [character set 编码方式 collate 校对规则] 显示库的创建信息:show create database name 数据库的删除:drop database [if exist]name 备份数据库:mysqldump -u用户名 -p数据库名>(路径)文件名.sql(windows命令.脚本文件,要退出sql命令窗口quit) 数据库恢复(1):Source 文件名.sql(sql命令)  

sqlserver row_number 类似 mysql中 limit 用法

select * from ( select row_number() over(ORDER BY inspecdate desc,inspectime DESC,itemorder asc ) as num, contentid,quesioncontext,tempid,tempname,itemid,itemtext,belongteam,teamname,inspecdate, inspectime,contenttext,createperson,newaddtime,updateti

MySQL replace into 用法(insert into 的增强版)

MySQL replace into 用法(insert into 的增强版) 在向表中插入数据的时候,经常遇到这样的情况:1. 首先判断数据是否存在: 2. 如果不存在,则插入:3.如果存在,则更新. 在 SQL Server 中可以这样处理: if not exists (select 1 from t where id = 1) insert into t(id, update_time) values(1, getdate()) else update t set update_time