The usual way to set up a self-add field:
Add when creating a table:
CREATE TABLE table1 (ID int auto_increment PRIMARY key,...)
Add after creating a table:
ALTER TABLE table1 Add ID int auto_increment primary key self-add field, be sure to set to primary key.
Cases
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:
| The code is as follows |
Copy Code |
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.
| The code is as follows |
Copy Code |
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:
| The code is as follows |
Copy Code |
Create_table:articles,: Options => ' auto_increment = 1001 ' Do |t|
# XXX Todo
End |
set the ID to start with n
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
| The code is as follows |
Copy Code |
TRUNCATE TABLE table1 |
Can