Read the full article
Fold
After setting the growing column, I insert the data, the growing column starts with 3, then 3, 6, 10, 13, 16, Shouldn't we start at one? And then we have 1, 4, 7, 10, and why is 6 to 10 off by 4?
The constructional sentences I use:
CREATE TABLE IF NOT EXISTS stuinfo2(
id INT PRIMARY KEY auto_increment,
stuname VARCHAR(20)
);
I set the step size to 3, and the initial value is 10(10 is arbitrary, I mean it doesn't work).
I insert only the value of stuname, leaving id empty in the insert statement.
Why does this happen?
0 Answer
Based on Monster group and GPT:
The initial values of the self-growing column do not come into play here. The initial values of the self-growing column only come into play if there is no data in the table.
As to why there is a 4 difference between 6 and 10, this is because when the third row is inserted, the value of the self-growing column is reset to the initial value of 10 because it is smaller than the set initial value. In this case, when the next row of data is inserted, the self-growing column increments from 10 instead of 9, resulting in a difference of 4 between 6 and 10.
To increment the self-growing column from 1 instead of 3, you can set the initial value of the self-growing column to 1 when creating the table, for example:
CREATE TABLE IF NOT EXISTS stuinfo2(
id INT PRIMARY KEY auto_increment,
stuname VARCHAR(20)
) AUTO_INCREMENT=1;
This way, the value of the growing column will increase from 1 instead of 3 each time the data is inserted.
Also, if you want to set the step size, you can use the following syntax when creating the table:
CREATE TABLE IF NOT EXISTS stuinfo2(
id INT PRIMARY KEY auto_increment,
stuname VARCHAR(20)
) AUTO_INCREMENT=1 INCREMENT=3;
Then, instead of increasing by 1, the value of each increment will increase by 3. Note that when setting the step size, the initial value of the self-growing column must be smaller than the step size to be valid, otherwise the step size setting is ignored.
这家伙很懒,什么都没留下...