Two Mysql methods to reorder self-increasing IDs from 1 _mysql

Source: Internet
Author: User
Tags first row

Recently always to initialize data for this project, make a lot of headaches, and the database ID become more and more, to let the self-increase starting from 1: Then use the following method:

method One: If the previous data is not needed, you can directly empty all the data, and the self-added field recovery from 1 start 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 it 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 error message will be generated when the table is referenced later.

Method Two will not empty the existing data, the operation is more flexible, not only can return the value of zero, but also for the deletion of a large number of consecutive rows, reset the value and insert new data, or starting from the 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);

The above is the whole content of this article, hope that these two methods for everyone's learning help.

Related Article

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.