Mysql self-Increasing ID starting value modification method _mysql

Source: Internet
Author: User

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:

TRUNCATE TABLE table1

Can

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.