How to reset automatic incremental columns of MySQL

Source: Internet
Author: User


How to reset the automatic incremental column of MySQL database provides a wonderful function AUTOINCREMENT column index. Your database table can be defined as a primary key with auto increment numbers. MySQL will take care of its unique value while inserting new rows. Every time www.2cto.com adds a new line, the MySQL increment value is automatically, and it sticks to the table. But sometimes you may need to reset the value of AUTOINCREMENT column to 1. You have written a sample application, and you have inserted several rows into the table. Now you need to delete these rows and auto increment column reset so that the new row will have the primary key value 1. There are several ways to achieve this. 1. Directly reset the auto increment value alter table Syntax provides a way to reset the auto increment column. Take a look at the example below. Alter table table_name AUTO_INCREMENT = 1; note that you cannot reset the counter value to be less than or equal to any used one. For MyISAM, if this value is smaller than or equal to the maximum value currently in the AUTO_INCREMENT column, this value is the largest plus one reset. For InnoDB, if the value is greater than the maximum value in the current column, no error occurs and the current sequence value is not changed. 2. The automatic increment value of the truncated table is automatically reset to 0. Truncate table table_name; use it with caution. When used for truncation, any AUTO_INCREMENT counter is reset to zero. From MySQL 5.0.13, The AUTO_INCREMENT counter resets the zero truncate table, regardless of whether there is a foreign key constraint. Once the TRUNCATE is initiated, the table does not remember the AUTO_INCREMENT value used in the past, but it is counted from the beginning. This is true. Even for MyISAM and InnoDB, sequence values are usually not reused. Www.2cto.com 3. Drop and re-create a table. This is another way to restart the auto increment index. Although not ideal. Drop table table_name; create table table_name {...}; value technologies of all these technologies reset the automatic incremental columns. Use any applicable requirements.

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.