Two methods for MySQL to reorder from 1 for self-increment ID

Source: Internet
Author: User

recently the current project to initialize the data, it is a headache, and the database ID from increasing, to let the self-increment starting from 1: Then use the following method:

Method One: If none of the previous data is needed, you can simply empty all data and restore the self-increment field from 1 to counting
TRUNCATE TABLE name

Method Two: DBCC CHECKIDENT (' table_name ', reseed, new_reseed_value) The current value is set to New_reseed_value. If a row has not been inserted into the table since the table was created, the first row inserted after the DBCC checkident is executed uses new_reseed_value as the identity. Otherwise, the next inserted row will use New_reseed_value + 1. If the value of new_reseed_value is less than the maximum value in the identity column, a No. 2627-number error message is generated later when the table is referenced.

method Two does not empty the existing data, the operation is more flexible, not only can the self-increment zero, but also for the deletion of a large number of consecutive rows, reset self-increment and insert new data, or start from a new value, of course, can not and existing conflicts.
$sql = "Delete from $table _vote";
mysql_query ($sql, $link);
$sql = "ALTER TABLE $table _vote auto_increment=1";
mysql_query ($sql, $link);


Two methods for MySQL to reorder from 1 for self-increment ID

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.