MySQL中的auto_increment

一、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: 0

mysql> 
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: 0

mysql> 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: 0

mysql> 
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: 0

mysql> 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设置为3

mysql> 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> 

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: auto_incrementMySQL的一个自增长字段。当新记录插入到表时,auto_increment字段的值会自动增加。这对于生成唯一的记录标识符很有用。在定义表时,可以使用auto_increment关键字来创建自增长字段。 <br>### 回答2: MySQLauto_increment是一种用于自动生成唯一标识符的特殊数据类型,常用于表的主键。auto_increment列必须是整数类型,它会自动递增并在下一个插入时生成一个新的唯一标识符。 使用auto_increment时,表只允许有一个自动编号列。当插入新行时,如果没有指定自动编号列的值,则系统会自动为其生成一个新的唯一标识符。如果已指定一个值,但该值已存在于表,则系统会自动为其生成另一个唯一标识符。 一般来说,使用auto_increment作为主键可以保证表的每一行都有唯一的标识符,并且可以方便地查询和更新表的数据。但需要注意的是,auto_increment列递增时会占用额外的空间,因此在设计表结构时应该合理考虑使用哪些列作为主键,同时也需要考虑表的性能。 <br>### 回答3: MySQLauto_increment是一种对表的一列(通常为主键)进行自动编号的特性。使用auto_increment时,每次插入新数据时,这个列的值会自动加1,从而保证每个数据行拥有唯一的主键。 auto_increment一般用于需要保证数据唯一性的表,如用户表、订单表等。在这些表,每个数据需要有唯一的标识符,以便对其进行更新、删除等操作。如果没有auto_increment,就需要手动指定主键的值,这会增加操作的复杂度,并且可能出现重复主键的错误,从而导致数据不一致。 使用auto_increment时,需要注意以下几点: 1. auto_increment只能用于整数类型的列,如int、bigint等。 2. auto_increment列必须是表的主键或唯一键。 3. 如果删除表的一行数据,auto_increment列的值不会回收,即下一次插入数据仍会使用上次被删除的行的auto_increment值。如果希望回收这些值,可以使用ALTER TABLE命令将auto_increment重置为当前最大值。 4. auto_increment是在插入数据时生成的,而不是在创建表时生成。因此,如果需要在创建表时指定初始值,可以使用ALTER TABLE命令。 总之,auto_incrementMySQL的一种重要特性,可以帮助用户轻松地保证数据的唯一性和完整性。虽然当表的数据量过大时,auto_increment的性能就会受到影响,但在大多数应用场景下,它仍然是一个非常实用且方便的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值