MySQL database self-increment ID re-ordered from 1 two methods

Source: Internet
Author: User
MySQL default self-increment ID is starting from 1, but when we have inserted a table or deleted ID by using delete, the ID will not start from 1.

When using MySQL, there is usually a self-increment ID field in the table, but when we want to empty the data in the table and re-add the data, we want the ID to be counted back from 1, in the following two ways:

The usual way to set the self-increment field:
Add when creating a table:


CREATE TABLE table1 (ID int auto_increment PRIMARY key,...)

After creating the table, add:


ALTER TABLE table1 Add ID int auto_increment primary key self-increment field, be sure to set to primary key.


Example


ALTER TABLE tablename drop column ID;
ALTER TABLE tablename Add ID mediumint (8) NOT NULL primary key auto_increment first;

Method Two:

ALTER TABLE TableName auto_increment=0

MySQL database self-increment ID re-ordered from 1 two methods

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.