MySQL (auto_increment) self-increasing ID's starting value modification and setting

Source: Internet
Author: User

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

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.