Setting the MySQL autogrowth method to start at a specified number

  • 2021-06-28 09:49:59
  • OfStack

Self-adding field, 1 must be set to primary key.

Take the example of specifying starting from 1000.

1 Set when the table is created:


CREATE TABLE `Test` ( 
     `ID` int(11) NOT NULL AUTO_INCREMENT, 
     `NAME` varchar(50) NOT NULL,  
     `SEX` varchar(2) NOT NULL, 
     PRIMARY KEY (`ID`)       
    ) ENGINE=MEMORY AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC 

2 If you did not set it when you created the table and want to set it later, you can do so by modifying:

alter table Test auto_increment = 1000;

Example 2:


create table user(  
id integer primary key,  
firstname varchar(200) not null,  
lastname varchar(200) not null);  
 
alter table user modify id integer auto_increment ;  
alter table user modify id integer default '1'; 

Sometimes you need to set auto_in mysqlThe fields sent by increment are reset (restore/restore), so in summary 1, there are probably three ways to do this:

1. Reset the value of autoIncrement directly

ALTER TABLE table_name AUTO_INCREMENT = 1;

2. Completed by truncate table

TRUNCATE TABLE table_name;

3. Delete the table and rebuild it

DROP TABLE table_name;
CREATE TABLE table_name { ... };


Related articles: