Method for modifying the start value of mysql auto-increment ID _ MySQL

Source: Internet
Author: User
Method for modifying the start value of mysql auto-increment ID: bitsCN.com

Many friends in mysql think that the field AUTO_INCREMENT type auto-increment ID value cannot be modified. In fact, this is an incorrect understanding. The following describes how to modify and set the starting value of mysql auto-increment ID.
The following describes how to set an auto-increment field:
Add the following when creating a table:

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

After creating a table, add:

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

In many cases, it is expected that the table data IDs do not start from 1, such as qq, and IDs start from 10000.
The code is as follows:

Alter table users AUTO_INCREMENT = 10000;

This statement can also be used to modify the id of an existing table. for example, if you want to return the id from 654321 to 123456 after deleting a large number of data

Alter table users AUTO_INCREMENT = 123456;

However, after actual tests, there is no problem with the single-host Mysql, and the Mysql Cluster is invalid. the mechanism on the primary key may be different. you have time to study it.

The syntax in Rails migration is as follows:

Create_table: articles,: options => 'Auto _ INCREMENT = 1001 'do | t |
# Xxx todo
End

Set auto-incrementing ID from N

Create table TABLE_1 (id int unsigned not null primary key AUTO_INCREMENT, // The ID column is an UNSIGNED integer. the column value cannot be empty, repeated, and auto-incrementing. Name varchar (5) not null) AUTO_INCREMENT = 100; (the ID column starts from 100)

If you want the auto-increment ID to start from the default value

Truncate table table1

You can.

BitsCN.com

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.