MySQL field self-increasing AUTO_INCREMENT study notes

1. Specify the initial value of the AUTO_INCREMENT self-increment (ie the starting value) when creating the table :

?
1
CREATE TABLE XXX (ID INT (5) PRIMARY KEY AUTO_INCREMENT) AUTO_INCREMENT=100;

2. Modify the initial value through ALTER TABLE (but it must be greater than the AUTO_INCREMENT self-increment in the table, otherwise the setting will be invalid):

?
1
ALTER TABLE XXX AUTO_INCREMENT=100;

3. If the maximum value of the auto-increment sequence is deleted, the value is reused when inserting a new record:

That is to say, if there are AUTO_INCREMENT attribute values ​​in the table with consecutive values ​​of 78 and 100, but the data of 100 has been deleted, when the data is added next, the auto-increment value is 101, and 100 is reused.

Even after you delete all data in the entire table, the previous maximum value of the auto-increment sequence will be reused.

The solution is:

Use ALTER TABLE XXX AUTO_INCREMENT=0; to reset the auto-increment initial value.

 


 

Set the AUTO_INCREMENT_INCREMENT and AUTO_INCREMENT_offset user variable values: (After restarting MySQL, these changes will revert to the initial value of 1)

?
1
2
3
SET auto_increment_increment=10;    #自增量每次增加的值改为10,
SET auto_increment_offset=2;    #第一次加载数值时的偏移值的个位值
SHOW VARIABLES LIKE 'AUTO_INC%' ;    #查看修改后变量的值

in addition:

mysql>SHOW TABLE STATUS FROM NAME_DB; -->Display information of all tables in database NAME_DB

mysql>SHOW CREATE TABLE NAME_TBL; -->Display information when table NAME_TBL was created

mysql>SHOW VARIABLES LIKE 'AUTO_INC%' --> Display MySQL's user session variables at the beginning of AUTO_INC (SHOW GLOBAL VARIABLES)

+--------------------------+-------+ 
| Variable_name                 | Value | 
+--------------------------+-------+ 
| auto_increment_increment|   1      | 
| auto_increment_offset      |   1      | 
+--------------------------+-------+

Change the names, attributes, etc. of the fields in the table:

mysql>ALTER TABLE XXX COL_NAME COL_NEW_NAME VARCHAR(20) [ELSE...]; #Rename the field COL_NAME in the table to COL_NEW_NAME

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326276964&siteId=291194637