How to modify the starting value of mysql auto-increment ID

Many friends in mysql think that the auto-increment ID value of the field type AUTO_INCREMENT cannot be modified. In fact, this understanding is wrong. The following describes the modification and setting method of the starting value of the mysql auto-increment ID.
The usual way to set auto-increment fields:
add when creating a table:

copy code code show as below:

create table table1(id int auto_increment primary key,...)


After creating the table add:

copy code code show as below:

alter table table1 add id int auto_increment primary key The auto-increment field must be set to primary key.


Many times it is hoped that the id of the data in the table does not start from 1. Like qq, the id starts from 10000. The
code is as follows:

copy code code show as below:

alter table users AUTO_INCREMENT=10000;
 


Moreover, this statement is also applicable to modifying the id of an existing table. For example, after deleting data in batches, the id will be returned from 654321 to 123456.

copy code code show as below:

alter table users AUTO_INCREMENT=123456;
 


But after the actual test, there is no problem with the single-machine Mysql, but it is invalid under Mysql Cluster, maybe the mechanism on the primary key is still different, I have time to study it

 

The way it is written in Rails migration is as follows:

copy code code show as below:

create_table :articles, :options => 'AUTO_INCREMENT = 1001' do |t|   
# xxx todo      
end

 

Set the auto-increment ID to start from N

copy code code show as below:

CREATE TABLE TABLE_1 ( ID INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, // ID列为无符号整型,该列值不可以为空,并不可以重复,而且自增。 NAME VARCHAR(5) NOT NULL ) AUTO_INCREMENT = 100;(ID列从100开始自增)


如果想让自增ID从默认值开始只要

复制代码代码如下:

TRUNCATE TABLE table1 
 


即可

Guess you like

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