MySQL 5.7 自增字段相关参数说明
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,auto_increment_increment 和 auto_increment_offset参数用在主主复制中,用于控制AUTO_INCREMENT字段的操作,在不同节点使用不同的生成规则,以避免
千家信息网最后更新 2025年02月02日MySQL 5.7 自增字段相关参数说明auto_increment_increment 和 auto_increment_offset参数用在主主复制中,用于控制AUTO_INCREMENT字段的操作,在不同节点使用不同的生成规则,以避免生成的序列相同而产生冲突。这两个参数可以分别设置全局和会话的变量,每个参数的值的范围是1~65535。将这两个参数设置为0会导致实际上将这两个参数的值设为1。
auto_increment_increment参数控制AUTO_INCREMENT字段值的间隔数
mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
2 rows in set (0.00 sec)
mysql> CREATE TABLE autoinc1
-> (col int not null auto_increment primary key)
-> ;
Query OK, 0 rows affected (0.12 sec)
mysql> select @@auto_increment_increment;
+----------------------------+
| @@auto_increment_increment |
+----------------------------+
| 1 |
+----------------------------+
1 row in set (0.00 sec)
mysql> set @@auto_increment_increment=10;
Query OK, 0 rows affected (0.06 sec)
mysql> select @@auto_increment_increment;
+----------------------------+
| @@auto_increment_increment |
+----------------------------+
| 10 |
+----------------------------+
1 row in set (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.00 sec)
mysql> INSERT INTO autoinc1 VALUES(null),(null),(null);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT col FROM autoinc1;
+-----+
| col |
+-----+
| 1 |
| 11 |
| 21 |
+-----+
3 rows in set (0.00 sec)
auto_increment_offset 参数决定AUTO_INCREMENT字段的起始值
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.00 sec)
mysql> CREATE TABLE autoinc2
-> (col int not null auto_increment primary key);
Query OK, 0 rows affected (0.64 sec)
mysql> INSERT INTO autoinc2 VALUES (null),(null),(null);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT col FROM autoinc2;
+-----+
| col |
+-----+
| 5 |
| 15 |
| 25 |
+-----+
3 rows in set (0.00 sec)
auto_increment_increment参数控制AUTO_INCREMENT字段值的间隔数
mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
2 rows in set (0.00 sec)
mysql> CREATE TABLE autoinc1
-> (col int not null auto_increment primary key)
-> ;
Query OK, 0 rows affected (0.12 sec)
mysql> select @@auto_increment_increment;
+----------------------------+
| @@auto_increment_increment |
+----------------------------+
| 1 |
+----------------------------+
1 row in set (0.00 sec)
mysql> set @@auto_increment_increment=10;
Query OK, 0 rows affected (0.06 sec)
mysql> select @@auto_increment_increment;
+----------------------------+
| @@auto_increment_increment |
+----------------------------+
| 10 |
+----------------------------+
1 row in set (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.00 sec)
mysql> INSERT INTO autoinc1 VALUES(null),(null),(null);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT col FROM autoinc1;
+-----+
| col |
+-----+
| 1 |
| 11 |
| 21 |
+-----+
3 rows in set (0.00 sec)
auto_increment_offset 参数决定AUTO_INCREMENT字段的起始值
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.00 sec)
mysql> CREATE TABLE autoinc2
-> (col int not null auto_increment primary key);
Query OK, 0 rows affected (0.64 sec)
mysql> INSERT INTO autoinc2 VALUES (null),(null),(null);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT col FROM autoinc2;
+-----+
| col |
+-----+
| 5 |
| 15 |
| 25 |
+-----+
3 rows in set (0.00 sec)
参数
字段
两个
不同
控制
生成
相同
上将
全局
变量
实际
序列
节点
范围
规则
冲突
起始
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
杭州服务贸易数据库
网络技术给企业带来的新挑战
数据库未来的发展趋势的是
福建数据库安全箱工程
服务器HPDL388G7
平川区妇联网络安全
软件开发变更控制管理
最先进的数据库入侵技术
用虚拟机还是云服务器好
一个软件开发的支付系统
广东数字桥网络技术有限公司
兰溪公司软件开发
神通数据库 dba
数据库先决条件检查失败
慧盈通网络技术
学习数据库的课程收获和体会
怎么给数据库增加文件组
关闭服务器代理
网络安全工作实施总结反馈
财务专业数据库学什么
软件开发售前的工作内容
服务器中VR散热器
c语言自学软件开发
启明星辰网络安全审计
加强100周年网络安全保障
水滴石穿打网络安全用语
大主宰数据库
全国网络安全大赛湖北
对数据库关系模式进行规范化
sql数据库分布式部署