Several methods of self-increment primary key zeroing in MySQL database

Source: Internet
Author: User

MySQL self-increment primary key zeroing method:

    1. If none of the previous data is needed, you can simply empty all the data and count the self-increment field recovery starting at 1:

TRUNCATE TABLE table_name;

2. When the user does not have TRUNCATE permissions and the data is not needed:

1) Delete the original primary key:

ALTER TABLE ' table_name ' DROP ' ID ';

2) Add a new primary key:

ALTER TABLE ' table_name ' ADD ' ID ' int (one) not NULL first;

3) Set the new primary key:

ALTER TABLE ' table_name ' MODIFY COLUMN ' id ' int (one) not NULL Auto_increment,add PRIMARY KEY (ID);

3. When the user does not have permission:

You can directly set the Auto_increment value of the data table to the desired initial value, such as 10000:

ALTER TABLE ' table_name ' auto_increment= 10000;


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.