mysql自增属性

auto_increment_increment

控制列中的值的增量值,比如:

mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+

mysql> CREATE TABLE auto_test (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (0.01 sec)

mysql> SET auto_increment_increment=5;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 5     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)

mysql> insert auto_test values (null),(null),(null),(null);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select id from auto_test;
+----+
| id |
+----+
|  1 |
|  6 |
| 11 |
| 16 |
+----+
4 rows in set (0.01 sec)
auto_increment_offset

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 | 5     |
| auto_increment_offset    | 5     |
+--------------------------+-------+
2 rows in set (0.00 sec)

mysql> CREATE TABLE auto_test1 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (0.01 sec)

mysql> insert auto_test1 values (null),(null),(null),(null);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql>  select id from auto_test1;
+----+
| id |
+----+
|  5 |
| 10 |
| 15 |
| 20 |
+----+
4 rows in set (0.00 sec)
值设置规则

1.auto_increment_offset > auto_increment_increment,mysq会忽略auto_increment_offset这个值.例如:

mysql> SET auto_increment_offset=10;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 5     |
| auto_increment_offset    | 10    |
+--------------------------+-------+
2 rows in set (0.00 sec)

mysql> CREATE TABLE auto_test3 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (0.00 sec)

mysql> insert auto_test3 values (NULL);
Query OK, 1 row affected (0.00 sec)

mysql> select * from auto_test3;
+----+
| id |
+----+
|  4 |
+----+
1 row in set (0.00 sec)

2.当这两个值有一个被改变时,AUTO_INCREMEN列值的计算方法.auto_increment_offset + N * auto_increment_increment (N >= 0).例如:

mysql> SET auto_increment_offset=2;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 5     |
| auto_increment_offset    | 2     |
+--------------------------+-------+
2 rows in set (0.00 sec)

mysql> select id from auto_test1;
+----+
| id |
+----+
|  5 |
| 10 |
| 15 |
| 20 |
+----+
4 rows in set (0.00 sec)

mysql> insert auto_test1 values (NULL), (null);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select id from auto_test1;
+----+
| id |
+----+
|  5 |
| 10 |
| 15 |
| 20 |
| 27 |
| 32 |
+----+
6 rows in set (0.00 sec)

第5条记录27没有按照自增规则,他是由上面的计算公式来计算的: 2 + 5 * 5 = 27

3.这两个属性是全局属性,不能被用于表级别.因此在mysql中不能通过多表设置不同起始值来实现序列.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值