文章目录

  • 一、AUTO_INCREMENT
    • 1.1 测试
  • 二、auto_increment_increment和auto_increment_offset
    • 2.1 解释
    • 2.2 测试
      • auto_increment_increment
      • auto_increment_offset
      • 当两者同时更改
  • 三、组复制中的group_replication_auto_increment_increment
    • 1.解释
    • 2.操作
      • 2.1 配置
      • 2.2 插数据

一、AUTO_INCREMENT

AUTO_INCREMENT 属性可用于为新行生成唯一标识

1.1 测试

#创建表
CREATE TABLE animals (id MEDIUMINT NOT NULL AUTO_INCREMENT,name CHAR(30) NOT NULL,PRIMARY KEY (id)
);
#插入数据
INSERT INTO animals (name) VALUES('dog'),('cat'),('penguin'),('lax'),('whale'),('ostrich');
#查看
SELECT * FROM animals;
+-----+-----------+
| id  | name      |
+-----+-----------+
|   1 | dog       |
|   2 | cat       |
|   3 | penguin   |
|   4 | lax       |
|   5 | whale     |
|   6 | ostrich   |

如果你的SQL_MODE没有设置NO_AUTO_VALUE_ON_ZERO SQL 模式。那就算你给自增列插入0或者null值,它还是会自增的,如下:

INSERT INTO animals (id,name) VALUES(0,'groundhog');
INSERT INTO animals (id,name) VALUES(NULL,'squirrel');
SELECT * FROM animals;
+-----+-----------+
| id  | name      |
+-----+-----------+
|   1 | dog       |
|   2 | cat       |
|   3 | penguin   |
|   4 | lax       |
|   5 | whale     |
|   6 | ostrich   |
|   7 | groundhog |
|   8 | squirrel  |

当你将任何其他值插入 AUTO_INCREMENT 列时,该列将设置为该值并重置序列,以便下一个自动生成的值从最大的列值开始。例如:

mysql> INSERT INTO animals (id,name) VALUES(100,'rabbit');
Query OK, 1 row affected (0.02 sec)mysql> INSERT INTO animals (id,name) VALUES(NULL,'mouse');
Query OK, 1 row affected (0.05 sec)mysql> SELECT * FROM animals;
+-----+-----------+
| id  | name      |
+-----+-----------+
|   1 | dog       |
|   2 | cat       |
|   3 | penguin   |
|   4 | lax       |
|   5 | whale     |
|   6 | ostrich   |
|   7 | groundhog |
|   8 | squirrel  |
| 100 | rabbit    |
| 101 | mouse     |
+-----+-----------+
10 rows in set (0.00 sec)mysql>

要从 1 以外的 AUTO_INCREMENT 值开始,请使用 CREATE TABLE 或 ALTER TABLE 设置该值,如下所示:

mysql> alter table animals auto_increment=100;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql>
mysql> INSERT INTO animals (name) VALUES('gengjin');
Query OK, 1 row affected (0.02 sec)mysql> SELECT * FROM animals;
+-----+-----------+
| id  | name      |
+-----+-----------+
|   1 | dog       |
|   2 | cat       |
|   3 | penguin   |
|   4 | lax       |
|   5 | whale     |
|   6 | ostrich   |
|   7 | groundhog |
|   8 | squirrel  |
| 100 | rabbit    |
| 101 | mouse     |
| 201 | gengjin   |
+-----+-----------+
11 rows in set (0.00 sec)

二、auto_increment_increment和auto_increment_offset

2.1 解释

auto_increment_increment 和 auto_increment_offset 用于循环(源到源)复制,可用于控制 AUTO_INCREMENT 列的操作。这两个变量都有全局值和会话值,并且每个变量都可以采用 1 到 65535 之间的整数值。将这两个变量中的任何一个的值设置为 0 会导致其值设置为 1。尝试将这两个变量中的任何一个的值设置为大于 65535 或小于 0 的整数会导致其值设置为 65535。尝试将 auto_increment_increment 或 auto_increment_offset 的值设置为非整数值会产生错误,而变量的实际值保持不变。

mysql> show variables like "%increm%";
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| auto_increment_increment                   | 3     |
| auto_increment_offset                      | 1     |
| div_precision_increment                    | 4     |
| group_replication_auto_increment_increment | 3     |
| innodb_autoextend_increment                | 64    |
+--------------------------------------------+-------+
5 rows in set (0.01 sec)mysql>

2.2 测试

auto_increment_increment 和 auto_increment_offset 影响 AUTO_INCREMENT 列行为如下:

auto_increment_increment

mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.01 sec)mysql> CREATE TABLE autoinc1(col INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (0.09 sec)mysql> SET @@auto_increment_increment=10;
Query OK, 0 rows affected (0.00 sec)mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 10    |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.01 sec)mysql> INSERT INTO autoinc1 VALUES (NULL), (NULL), (NULL), (NULL);
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0mysql> SELECT col FROM autoinc1;
+-----+
| col |
+-----+
|   1 |
|  11 |
|  21 |
|  31 |
+-----+
4 rows in set (0.00 sec)mysql>

auto_increment_offset

mysql> SET @@auto_increment_offset=5;
Query OK, 0 rows affected (0.00 sec)mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 10    |
| auto_increment_offset    | 5     |
+--------------------------+-------+
2 rows in set (0.01 sec)mysql>
mysql> CREATE TABLE autoinc2(col INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (0.08 sec)mysql>
mysql> INSERT INTO autoinc2 VALUES (NULL), (NULL), (NULL), (NULL);
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0mysql>
mysql> SELECT col FROM autoinc2;
+-----+
| col |
+-----+
|   5 |
|  15 |
|  25 |
|  35 |
+-----+
4 rows in set (0.00 sec)mysql>

当两者同时更改

当auto_increment_offset 的值大于auto_increment_increment 的值时,忽略auto_increment_offset 的值。

mysql> show variables like "auto_inc%";
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 10    |
| auto_increment_offset    | 5     |
+--------------------------+-------+
2 rows in set (0.00 sec)
mysql> SELECT col FROM autoinc1;
+-----+
| col |
+-----+
|   1 |
|  11 |
|  21 |
|  31 |
+-----+
4 rows in set (0.00 sec)mysql> INSERT INTO autoinc1 VALUES (NULL), (NULL), (NULL), (NULL);
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0mysql> SELECT col FROM autoinc1;
+-----+
| col |
+-----+
|   1 |
|  11 |
|  21 |
|  31 |
|  45 |
|  55 |
|  65 |
|  75 |
+-----+
8 rows in set (0.00 sec)mysql>

三、组复制中的group_replication_auto_increment_increment

1.解释

服务器上启动Group Replication时,将auto_increment_increment的值改为group_replication_auto_increment_increment的值,默认为7,auto_increment_offset的值改为server_ID。

mysql> show variables like "%auto_increm%";
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| auto_increment_increment                   | 3     |
| auto_increment_offset                      | 1     |
| group_replication_auto_increment_increment | 3     |
+--------------------------------------------+-------+
3 rows in set (0.00 sec)mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)mysql>

当组复制停止时,更改将恢复。仅当 auto_increment_increment 和 auto_increment_offset 的默认值都为 1 时,才会进行和恢复这些更改。如果它们的值已经从默认值进行了修改,则 Group Replication 不会更改它们。
从 MySQL 8.0 开始,当 Group Replication 处于单主模式时,系统变量也不会被修改,其中只有一个服务器写入。

2.操作

2.1 配置

##我们现在在搭建一个mgr多主的一个架构,初始数值如下:
所有节点均如下
mysql> show variables like "%auto_increm%";
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| auto_increment_increment                   | 1     |
| auto_increment_offset                      | 1     |
| group_replication_auto_increment_increment | 7     |
+--------------------------------------------+-------+
3 rows in set (0.01 sec)mysql> 将group_replication_auto_increment_increment设置为3mysql> set global group_replication_auto_increment_increment=3;
Query OK, 0 rows affected (0.00 sec)mysql>#启动mgr
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)mysql> start group_replication;
Query OK, 0 rows affected (3.19 sec)mysql> SET GLOBAL group_replication_bootstrap_group=off;
Query OK, 0 rows affected (0.00 sec)#我们将auto_increment_increment 改为3,将auto_increment_offset改为server_id(mgr三节点分别为1,2,3)
PS:理应当启动mgr的时候auto_increment_increment会自动变成group_replication_auto_increment_increment的值,auto_increment_offset变为server_id。这里加上手动修改方式。三节点,改法均如下
mysql> set session auto_increment_offset=2;
Query OK, 0 rows affected (0.00 sec)mysql> set session auto_increment_increment=3;
Query OK, 0 rows affected (0.00 sec)mysql> show variables like "%auto_increm%";
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| auto_increment_increment                   | 3     |
| auto_increment_offset                      | 2     |
| group_replication_auto_increment_increment | 3     |
+--------------------------------------------+-------+
3 rows in set (0.00 sec)mysql> 

2.2 插数据

#建表
CREATE TABLE `incre_tb0` (`increment_id` int NOT NULL AUTO_INCREMENT COMMENT '自增主键',`stu_id` int NOT NULL COMMENT '学号',`stu_name` varchar(20) DEFAULT NULL COMMENT '学生姓名',`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',PRIMARY KEY (`increment_id`),UNIQUE KEY `uk_stu_id` (`stu_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测试自增主键';#1节点插入数据
mysql> insert into incre_tb0 (stu_id,stu_name) values (1001,'gengjin');
Query OK, 1 row affected (0.01 sec)mysql> insert into incre_tb0 (stu_id,stu_name) values (1002,'kobe');
Query OK, 1 row affected (0.01 sec)
#2节点插入数据
mysql> insert into incre_tb0 (stu_id,stu_name) values (1003,'jordan');
Query OK, 1 row affected (0.00 sec)mysql> insert into incre_tb0 (stu_id,stu_name) values (1004,'har');
Query OK, 1 row affected (0.00 sec)
#3节点插入数据
mysql> insert into incre_tb0 (stu_id,stu_name) values (1005,'weishao');
Query OK, 1 row affected (0.00 sec)mysql> insert into incre_tb0 (stu_id,stu_name) values (1006,'gg');
Query OK, 1 row affected (0.00 sec)#查询
mysql> select * from incre_tb0;
+--------------+--------+----------+---------------------+---------------------+
| increment_id | stu_id | stu_name | create_time         | update_time         |
+--------------+--------+----------+---------------------+---------------------+
|            1 |   1001 | gengjin  | 2021-08-30 15:31:44 | 2021-08-30 15:31:44 |
|            4 |   1002 | kobe     | 2021-08-30 15:32:05 | 2021-08-30 15:32:05 |
|            5 |   1003 | jordan   | 2021-08-30 15:33:04 | 2021-08-30 15:33:04 |
|            8 |   1004 | har      | 2021-08-30 15:33:14 | 2021-08-30 15:33:14 |
|            9 |   1005 | weishao  | 2021-08-30 15:33:28 | 2021-08-30 15:33:28 |
|           12 |   1006 | gg       | 2021-08-30 15:33:39 | 2021-08-30 15:33:39 |
+--------------+--------+----------+---------------------+---------------------+
6 rows in set (0.00 sec)mysql> 

MySQL中的auto_increment相关推荐

  1. 如何在MySQL中重置AUTO_INCREMENT?

    本文翻译自:How to reset AUTO_INCREMENT in MySQL? How can I reset the AUTO_INCREMENT of a field? 如何重置字段的AU ...

  2. mysql auto_increment 重置_如何重置MySQL中的AUTO_INCREMENT?

    你可以用下面的方法重置计数器 ALTER TABLE tablename AUTO_INCREMENT = 1 对于InnoDB,您不能将auto_increment值设置为低于或等于最高当前索引. ...

  3. mysql中自增auto_increment功能的相关设置及问题

    1. mysql中的自增auto_increment功能相信每位phper都用过,本文就为大家分享一下mysql字段自增功能的具体查看及设置方法 mysql中的自增auto_increment功能相信 ...

  4. MySQL 中 AUTO_INCREMENT 的“坑” --重复值问题

    MySQL · 捉虫动态· InnoDB自增列重复值问题 问题重现 先从问题入手,重现下这个 bug use test; drop table if exists t1; create table t ...

  5. mysql 中auto_mysql中的auto_increment

    在构建数据库应用时,经常会遇到这样的情景:我们需要一个唯一的整数标号(id)来标识一条记录,但显然我们不想在插入一条记录之前还要先遍历一次整个表,然后确定一个合适的值来做为这条要插入记录的唯一标号,因 ...

  6. mysql数据库 auto_increment_MYSQL数据库中的auto_increment

    您的位置:首页 - MYSQL - 正文 MYSQL数据库中的auto_increment 在MySQL中,经常使用 AUTO_INCREMENT对主键建立自增id的行为,MySQL会自行保证主键ID ...

  7. mysql alter auto increment_修改mysql中Auto_increment值的例子

    要求: 修改mysql中某张表的下一条记录的Auto_increment值. 操作方法: 查看db.table表的下一条记录auto_increment的值: show table status fr ...

  8. mysql数据库 auto_increment_mysql学习笔记(二:中的auto_increment 理解

    1.auto_increment 理解1 auto_increment是用于主键自动增长的,从1开始增长,当你把第一条记录删除时,再插入第二跳数据时,主键值是2,不是1. 例如: create tab ...

  9. mysql中关系怎么弄_mysql数据库关系操作

    ### mysql数据库 #### 知识要点: 1. 数据操作增.删.改 2. 外键约束要求 3. 一对多表关系 4. 一对一表关系 5. 多对多表关系 6. 外键约束的参照操作 ### 数据操作 # ...

最新文章

  1. 如何实现一个连接池?一文带你深入浅出,彻底搞懂!
  2. java数字转中文_Java程序:输入数字转换成中文输出
  3. python画柱形图-Python绘制柱状图
  4. 使用Ubuntu 12.04作为日常电脑环境
  5. c++内存中字节对齐问题详解
  6. 关于TP-LINK宽带路由器上的“转发规则”功能用途及设置办法
  7. mikrotik ros ***借线
  8. mysql 建索引_mysql数据库正确建立索引及使用
  9. LiveVideoStack线上交流分享 (十) —— 开源声码器WORLD在语音合成中的应用
  10. 知乎高赞:自控力极差的人如何自救?
  11. 【转载保存】大型推荐系统架构图设计图
  12. 产生式模型和判别式模型
  13. 处理Excel电子表格
  14. 专题八:MATLAB图形用户界面设计
  15. 10个最新交互式Web设计实例欣赏
  16. 最新的ESD(ElectroStatic Discharge)静电介绍及其标准下载(见文尾)
  17. 一枚菜鸟前端工程师月度工作总结
  18. C++ primer 个人学习总结
  19. 信息化项目中服务器费用占比,企业信息化投入是什么 它的占比是多少
  20. 14.1 来自Bitly的USA.gov的数据(2)

热门文章

  1. windows系统运行uniapp到ios基座
  2. cfd-post 流线很少
  3. 所谓“螺旋方阵”,是指对任意给定的N,将1到N×N的数字从左上角第1个格子开始,按顺时针螺旋方向顺序填入N×N的方阵里。本题要求构造这样的螺旋方阵。
  4. 前端面试题——计算机网络 高频
  5. 邮件工具-MailUtil(发送邮件)
  6. Android播放音频到耳机,Android音乐播放模式切换-外放、听筒、耳机
  7. 问题1053:素数回文
  8. 【Google Chrome】如何让浏览器显示手机网页
  9. 微信小程序云开发—校园动物图鉴
  10. 基于Python的个人足迹地图绘制设计