MySQL的系统变量或会话变量auto_increment_increment(自增步长)和auto_increment_offset(自增偏移量)控制着数据表的自增列ID。
  
  mysql> show tables;
  
  Empty set (0.00 sec)
  
  mysql> CREATE TABLE `test_tb1` ( `id` int(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(20)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  
  Query OK, 0 rows affected (0.16 sec)
  
  mysql> insert into test_tb1(name) values('Andy'),('Danny');
  
  Query OK, 2 rows affected (0.13 sec)
  
  Records: 2 Duplicates: 0 Warnings: 0
  
  mysql> select * from test_tb1;
  
  +----+-------+
  
  | id | name |
  
  +----+-------+
  
  | 1 | Andy |
  
  | 2 | Danny |
  
  +----+-------+
  
  2 rows in set (0.04 sec)
  
  mysql> show session variables like '%auto_incre%';
  
  +--------------------------+-------+
  
  | Variable_name | Value |
  
  +--------------------------+-------+
  
  | auto_increment_increment | 1 |
  
  | auto_increment_offset | 1 |
  
  +--------------------------+-------+
  
  2 rows in set (0.01 sec)
  
  mysql> set session auto_increment_increment=10;
  
  Query OK, 0 rows affected (0.00 sec)
  
  mysql> show session variables like '%auto_incre%'; -- 改变自增步长
  
  +--------------------------+-------+
  
  | Variable_name | Value |
  
  +--------------------------+-------+
  
  | auto_increment_increment | 10 |
  
  | auto_increment_offset | 1 |
  
  +--------------------------+-------+
  
  2 rows in set (0.01 sec)
  
  mysql> show global variables like '%auto_incre%'; -- 全局变量并没有变化
  
  +--------------------------+-------+
  
  | Variable_name | Value |
  
  +--------------------------+-------+
  
  | auto_increment_increment | 1 |
  
  | auto_increment_offset | 1 |
  
  +--------------------------+-------+
  
  2 rows in set (0.05 sec)
  
  mysql> truncate table test_tb1;
  
  Query OK, 0 rows affected (0.09 sec)
  
  mysql> insert into test_tb1(name) values('Andy'),('Bonny'),('Lisa'),('Jack'),('Robin');
  
  Query OK, 5 rows affected (0.06 sec)
  
  Records: 5 Duplicates: 0 Warnings: 0
  
  mysql> select * from test_tb1;
  
  +----+-------+
  
  | id | name |
  
  +----+-------+
  
  | 1 | Andy |
  
  | 11 | Bonny |
  
  | 21 | Lisa |
  
  | 31 | Jack |
  
  | 41 | Robin |
  
  +----+-------+
  
  5 rows in set (0.00 sec)
  
  获取指定数据表的下一个auto_increment自增值的两种方式:
  
  mysql> show table status \G
  
  *************************** 1. row ***************************
  
  Name: test_tb1
  
  Engine: InnoDB
  
  Version: 10
  
  Row_format: Dynamic
  
  Rows: 5
  
  Avg_row_length: 3276
  
  Data_length: 16384
  
  Max_data_length: 0
  
  Index_length: 0
  
  Data_free: 0
  
  Auto_increment: 51
  
  Create_time: 2018-12-19 14:43:53
  
  Update_time: 2018-12-19 14:54:23
  
  Check_time: NULL
  
  Collation: utf8_general_ci
  
  Checksum: NULL
  
  Create_options:
  
  Comment:
  
  1 row in set (0.00 sec)
  
  mysql> select auto_increment from information_schema.`TABLES` where table_name='test_tb1';
  
  +----------------+
  
  | auto_increment |
  
  +----------------+
  
  | 51 |
  
  +----------------+
  
  1 row in set (0.03 sec)
  
  auto_increment_increment和auto_increment_offset的取值范围: 1 ~ 65535,这个范围和自增列的值没关系,列的数据大小是对应的值类型控制的。
  
  mysql> set auto_increment_yongshiyule178.com increment=-1;
  
  Query OK, 0 rows affected, 1 warning (0.05 sec)
  
  mysql> show variables like '%auto_incre%'; -- 小于1时用默认最小值1
  
  +--------------------------+-------+
  
  | Variable_name | Value |
  
  +--------------------------+-------+
  
  | auto_increment_increment | 1 |
  
  | auto_increment_offset | 1 |
  
  +--------------------------+-------+
  
  2 rows in set (0.00 sec)
  
  mysql> set auto_increment_www.mcyllpt.com increment=65536;
  
  Query OK, 0 rows affected, 1 warning (0.00 sec)
  
  mysql> show variables like '%auto_incre%'; -- 大于65535时用默认最大值65536
  
  +--------------------------+-------+
  
  | Variable_name | Value |
  
  +--------------------------+-------+
  
  | auto_increment_increment | 65535 |
  
  | auto_increment_offset | 1 |
  
  +--------------------------+-------+
  
  2 rows in set (0.00 sec)
  
  mysql> truncate table test_tb1;
  
  Query OK, 0 rows affected (0.49 sec)
  
  mysql> insert into test_tb1(name) values('Andy'), ('Bonny'), ('Danny'), ('Jack'), ('Robin');
  
  Query OK, 5 rows affected (0.01 sec)
  
  Records: 5 Duplicates: 0 Warnings: 0
  
  mysql> select * from test_www.yigouyule2.cn tb1; -- 值可以大于65535
  
  +--------+-------+
  
  | id | name |
  
  +--------+-------+
  
  | 1 | Andy |
  
  | 65536 | Bonny |
  
  | 131071 | Danny |
  
  | 196606 | Jack |
  
  | 262141 | Robin |
  
  +--------+-------+
  
  5 rows in set (0.00 sec)
  
  mysql> truncate table test_tb1;
  
  Query OK, 0 rows www.michenggw.com affected (0.06 sec)
  
  mysql> insert into test_tb1(id, name) values(-3, 'Andy'), (-2, 'Bonny'), (-1, 'Danny');
  
  Query OK, 3 rows affected (0.06 sec)
  
  Records: 3 Duplicates: 0 Warnings: 0
  
  mysql> select * from test_tb1; -- 值可以小于1
  
  +----+-------+
  
  | id | name |
  
  +----+-------+
  
  | -3 | Andy |
  
  | -2 | Bonny |
  
  | -1 | Danny |
  
  +----+-------+
  
  3 rows in set (0.00 sec)
  
  全局(global)与会话(session)自增属性,会话针对当前连接的所有操作,全局变量会在新的连接会话中生效。
  
  mysql> show global variables like 'auto_incre%'; -- 全局的自增属性值
  
  +--------------------------+-------+
  
  | Variable_name | Value |
  
  +--------------------------+-------+
  
  | auto_increment_increment | 1 |
  
  | auto_increment_offset | 1 |
  
  +--------------------------+-------+
  
  2 rows in set (0.01 sec)
  
  mysql> show variables like 'auto_incre%'; -- 会话的自增属性值,同show session variables like 'auto_incre%';
  
  +--------------------------+-------+
  
  | Variable_name | Value |
  
  +--------------------------+-------+
  
  | auto_increment_increment | 65535 |
  
  | auto_increment_offset | 1 |
  
  +--------------------------+-------+
  
  2 rows in set (0.00 sec)
  
  mysql> set global auto_increment_increment=10;
  
  Query OK, 0 rows affected (0.00 sec)
  
  mysql> show variables like 'auto_incre%';
  
  +--------------------------+-------+
  
  | Variable_name | Value |
  
  +--------------------------+-------+
  
  | auto_increment_increment | 65535 |
  
  | auto_increment_offset | 1 |
  
  +--------------------------+-------+
  
  2 rows in set (0.01 sec)
  
  mysql> show global variables like 'auto_incre%';
  
  +--------------------------+-------+
  
  | Variable_name | Value |
  
  +--------------------------+-------+
  
  | auto_increment_increment | 10 |
  
  | auto_increment_offset | 1 |
  
  +--------------------------+-------+
  
  2 rows in set (0.00 sec)

MySQL自增属性auto_increment_increment和auto_increment_offset的更多相关文章

  1. Mysql设置auto_increment_increment和auto_increment_offset

    查看与设置: show variables like '%auto_inc%'; show session variables like '%auto_inc%'; -- //session会话变量 ...

  2. MySQL系统变量auto_increment_increment与auto_increment_offset学习总结

    在MySQL中,系统变量auto_increment_increment与auto_increment_offset是与自增列相关的两个参数变量.在官方文档中,将其划分为Replication Mas ...

  3. MySQL auto_increment_increment 和 auto_increment_offset

    参考这一篇文章:(不过我对这一篇文章有异议) http://blog.csdn.net/leshami/article/details/39779509 1:搭建测试环境 create table t ...

  4. mysql为字段添加或删除自增属性

    删除自增属性: ALTER TABLE `members` CHANGE uid uid INT(10) UNSIGNED NOT NULL ; 添加自增属性: ALTER TABLE `member ...

  5. MySql id 设定为主键不自增后,再给 sort 字段增加自增属性

    需求 id 已经被设置为主键,但是没有给它设置 自增 属性.sort 起到一个排序的作用,需要给它设置一个 自增 属性 加自增属性的前提 表中的属性没有增加自增 赋予自增属性的字段,必须带有 索引 S ...

  6. mysql 的增删改查

    数据库的基本流程就是先看你的数据库中的库都是哪些:show databases; 然后再进入相应的库进行操作  :  use+进入的库/表 切换路径 查看这个库内的所有的表: show tabales ...

  7. Java连接MySQL数据库增删改查通用方法

    版权声明:本文为博主原创文章,未经博主允许不得转载. Java连接MySQL数据库增删改查通用方法 运行环境:eclipse+MySQL 以前我们Java连接MySQL数据库都是一个数据库写一个类,类 ...

  8. Asp.Net操作MySql数据库增删改查

    Asp.Net操作MySql数据库增删改查,话不多说直接步入正题.git源码地址:https://git.oschina.net/gxiaopan/NetMySql.git  1.安装MySQL数据库 ...

  9. mybatis的执行流程 #{}和${} Mysql自增主键返回 resultMap 一对多 多对一配置

    n Mybatis配置 全局配置文件SqlMapConfig.xml,配置了Mybatis的运行环境等信息. Mapper.xml文件即Sql映射文件,文件中配置了操作数据库的Sql语句.此文件需要在 ...

随机推荐

  1. Chrome Google 快捷键

    窗口和标签页快捷方式 Ctrl+N 打开新窗口 按住 Ctrl‎ 键,然后点击链接 在新标签页中打开链接 按住 Shift 键,然后点击链接 在新窗口中打开链接 Alt+F4 关闭当前窗口 Ctrl+ ...

  2. vue 数组数据更新或者对象数据更新 但是页面没有同步问题

    1,使用set函数来设置数据. 2,你可以通过 $forceUpdate 来做这件事.在数据赋值之后 就直接调用 this.$forceUpdata()

  3. mysql 常用函数,基本使用

    1:选中排除表1 连接表2 表3 获取选中表1中部分选中表3 的部分 并且设置选中状态select t1.*,if(t2中t3id=t1.id,1,0)as checked from t1 lefet ...

  4. Nodejs 使用 addons 调用c++ 初体验(一)

    纠结很久,决定写一点遇到的“坑”. 基础环境:win7-64bit  node(v7.5.0)   这些安装实在是太方便了,自行准备吧. 1. 安装 python(2.7.x ),用npm安装 nod ...

  5. C++ 求阶乘

    #include<iostream> using namespace std; ; //输入阶乘数 int main() { long long factorial[size]; fact ...

  6. Waterline从概念到实操

    Waterline基本介绍 Waterline是什么 Waterline是下一代存储和检索引擎,也是Sails框架中使用的默认ORM . ORM的基本概念 Object Relational Mapp ...

  7. vuejs中的计算属性和监视

    计算属性 1.在computed属性对象中定义计算属性的方法,在页面上使用{{方法名}}来显示计算结果 2.通过getter/setter实现对属性数据的显示和监视 3.计算属性存在缓存,多次读取只执 ...

  8. Sql Server 表间对应关系

    <1>.关联映射:一对多/多对一 存在最普遍的映射关系,简单来讲就如球员与球队的关系:一对多:从球队角度来说一个球队拥有多个球员 即为一对多多对一:从球员角度来说多个球员属于一个球队 即为 ...

  9. leetcode笔记--6 Add Digits

    question: Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  10. iOS笔记058 - IOS之多线程

    IOS开发中多线程 主线程 一个iOS程序运行后,默认会开启1条线程,称为"主线程"或"UI线程" 作用 显示和刷新界面 处理UI事件(点击.滚动.拖拽等) 注 ...