Bootstrap

MySQL修改自动增长值10无效_设置主键自增,初始值从10开始,每次自增10 原创

一、首先题主提问的标题会让人误解

初始值从10开始,每次自增10

根据截图反推回去,最开始的auto_increment_%变量应该是这样的↓

e4f794df1c90c96a0ea7121ffbeebb78.png

顺便纠正下@eason_li 的解释,这两个变量正确的含义:

auto_increment_increment:列的增量值,这才是步长。

auto_increment_offset:初始值,也称偏移量。

因为题主CREATE TABLE后马上更改了变量SET auto_increment_increment=10;,此时变量应该是这样的↓

88ab513e96a15e55daed6e4a73f648ce.png

也就是初始值还是1,但每次自增变成了10。也就不是上面有人说的从10开始,然后+1

二、自增列值的计算始终是这样,没错↓

auto_increment_offset + N × auto_increment_increment

但由于更改了其中一个变量,N的范围变成了[1,2,3...]中的正整数

再引用下MySQL官方文档

If either of these variables is changed, and then new rows inserted into a table containing an AUTO_INCREMENT column, the results may seem counterintuitive because the series of AUTO_INCREMENT values is calculated without regard to any values already present in the column, and the next value inserted is the least value in the series that is greater than the maximum existing value in the AUTO_INCREMENT column. The series is calculated like this:

auto_increment_offset + N × auto_increment_increment

where N is a positive integer value in the series [1, 2, 3, ...]

结合一中查出来的变量值代入,自增列的值序列应该是这样的[11,21,31...],由于create table的时候后面还加了个AUTO_INCREMENT=9;那么当前插入的自增列值应该是要大于9,但在自增列的值序列中是最小的,也就是11。

(例如create table的时候如果定义AUTO_INCREMENT=19,那就是要大于19,最小的值就是21了~)

;