Reset the auto-increment ID of mysql (re-sort the ID of Zhimeng article)

Source: Internet
Author: User


When you do a Web site, you often encounter the article ID is not continuous because of the deletion of the article, because the article ID field in the database properties are generally automatic growth, this is a register in MySQL records, when the deletion of records added, the value of the increment will not occupy your deleted values, but continue on the original base + +.

Discontinuous records in a datasheet are intolerable for Virgo, so let's look at how to rearrange the ID of an existing data.
Here in the Dream System (DEDECMS) Article table as an example: suppose the table to be manipulated is dede_archives, which is the main table of the content of the dream-weaving.
1. Create a new field on the table you want to manipulate to store the current ID.

ALTER TABLE dede_archives add old_id int (a) not null;

2. Save the current ID to this new field.

Update dede_archives set Old_id=id;

3. Deletes the ID field of the current table (the self-added primary key).

ALTER TABLE dede_archives drop ID;

4. Re-establish the ID field, the data type can be as it was originally.

ALTER TABLE dede_archives add ID int (a) not null;

5. Set the properties of this new ID field, set to the self-add and primary key.

ALTER TABLE dede_archives modify column ID int (a) NOT NULL auto_increment, add primary key (ID);

After this step, the new ID field has been rearranged ~
The following can be updated with other associated tables, which is the effect of creating that old_id.

Update Dede_archives as a, Dede_ vice table as B set b.aid=a.id where b.aid=a.old_id;

7. Finish it! Now the article ID has been counted from 1.

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.