mysql 自定义变量自增_mysql自定义表自增id步长

本文介绍了如何在MySQL中实现单个表的自增ID步长自定义,通过设置会话级别的auto_increment_increment变量,可以改变插入数据时自增ID的增长步长,而不会影响全局或其他会话的步长。
摘要由CSDN通过智能技术生成

今天有个兄弟有个需求,需要单个表的自增id,增长步长为4,

之前配置主主环境的时候有两个参数控制步长和偏移量auto_increment_increment=2,auto_increment_offset=1,但这个是全局的并写在配置文件里面

现用下面的方法,实现session级别的自增id步长自定义

先看到,系统全局环境的自增情况,默认为1

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> select * from test;

Empty set (0.00 sec)

mysql> insert into test values();

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;

+----+

| id |

+----+

| 1 |

+----+

1 row in set (0.00 sec)

mysql> insert into test values();

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;

+----+

| id |

+----+

| 1 |

| 2 |

+----+

2 rows in set (0.00 sec) 然后我们在当前命令行下设置auto_increment_increment变量并插入两条数据试试

mysql> SET auto_increment_increment=4;

Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'auto_inc%';

+--------------------------+-------+

| Variable_name | Value |

+--------------------------+-------+

| auto_increment_increment | 4 |

| auto_increment_offset | 1 |

+--------------------------+-------+

2 rows in set (0.00 sec)

mysql> insert into test values();

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;

+----+

| id |

+----+

| 1 |

| 2 |

| 5 |

+----+

3 rows in set (0.00 sec)

mysql> insert into test values();

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;

+----+

| id |

+----+

| 1 |

| 2 |

| 5 |

| 9 |

+----+

4 rows in set (0.00 sec)

mysql>

看到步长已经变成4了

现在另外开一个命令窗口,测试其他session下步长增长情况

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

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> select * from test;

+----+

| id |

+----+

| 1 |

| 2 |

| 5 |

| 9 |

+----+

4 rows in set (0.00 sec)

mysql> insert into test values();

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;

+----+

| id |

+----+

| 1 |

| 2 |

| 5 |

| 9 |

| 11 |

+----+

5 rows in set (0.00 sec)

mysql> insert into test values();

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;

+----+

| id |

+----+

| 1 |

| 2 |

| 5 |

| 9 |

| 11 |

| 12 |

+----+

6 rows in set (0.00 sec)

mysql> insert into test values();

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;

+----+

| id |

+----+

| 1 |

| 2 |

| 5 |

| 9 |

| 11 |

| 12 |

| 13 |

+----+

7 rows in set (0.00 sec)

看到步长还是默认的1,,插入的数据也验证了这一点。

现在退出一开始的session,重新进命令行,查看该参数情况

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 307981587

Server version: 5.5.8-log Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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>

参数又回到全局默认。

备注:该方法中,该session级别操作的所有表的id自增情况将为+4,如果操作的表对这方面有要求的,需谨慎!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值