0
Follow
0
View

Questions about self-growing columns in mysql

daybreak30 注册会员
2023-02-28 21:08
domeki 注册会员
2023-02-28 21:08

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.

About the Author

Question Info

Publish Time
2023-02-28 21:08
Update Time
2023-02-28 21:08