In MySQL, many friends think that the field Auto_increment type ID value can not be modified, in fact, this understanding is wrong, the following describes the MySQL ID's starting value modification and setting method.
The usual way to set up a self-add field:
Add when creating a table:
Copy Code code as follows:
CREATE TABLE table1 (ID int auto_increment PRIMARY key,...)
Add after creating a table:
Copy Code code as follows:
ALTER TABLE table1 Add ID int auto_increment primary key self-add field, be sure to set to primary key.
Many times you want the ID of the data in table not to start with 1, like QQ, where ID starts with 10000
The code is as follows:
Copy Code code as follows:
ALTER TABLE users auto_increment=10000;
The statement also applies to modifying the ID of an existing table, such as deleting data in bulk, and then returning the ID from 654321 to 123456.
Copy Code code as follows:
ALTER TABLE users auto_increment=123456;
But after the actual test, stand-alone MySQL no problem, MySQL cluster is invalid, may be on the primary key mechanism, or different, have time to study
The rails migration in the following notation:
Copy Code code as follows:
Create_table:articles,: Options => ' auto_increment = 1001 ' Do |t|
# XXX Todo
End
Set the ID to start with n
Copy Code code as follows:
CREATE TABLE table_1 (id INT UNSIGNED not NULL PRIMARY KEY auto_increment,//ID is listed as an unsigned integer, the column value cannot be empty, cannot be duplicated, and is self added. NAME VARCHAR (5) not NULL) Auto_increment = (id column starts from 100)
If you want your self-ID to start at the default value, just
Copy Code code as follows:
Can