Home  >  Article  >  Database  >  How to reset the initial value of the auto-increment column in the table in MySQL and share the implementation method

How to reset the initial value of the auto-increment column in the table in MySQL and share the implementation method

黄舟
黄舟Original
2017-03-24 13:21:411626browse

This article mainly introduces the relevant information on the implementation method of resetting the initial value of the auto-increment column in the table in MySQL. Friends in need can refer to the following

How to implement the initial value of the auto-increment column in the table in MySQL

1. Question raised

In the database design of MySQL, it is generally designed An auto-incrementing numeric column used as a business-independent primary key. After frequent deletion or clearing operations in the database, its self-increasing value will still increase automatically. What should I do if I need to start over?

2. Solution

a. alter table

delete from table_name; 
ALTER TABLE table_name AUTO_INCREMENT = 1;

If there is a lot of data in the database table, the deletion operation will last for a long time. This Issues require attention.

b. truncate

truncate table_name

Simple and fast, clear the data directly.

3. Delete vs tuncate

The main differences are as follows:

  • Truncate is fast and does not log record, so the rollback operation cannot be performed. delete and vice versa.

  • truncate will reset the index and auto-increment the initial value, delete will not

  • truncate will not trigger the trigger, but delete will. .

The above is the detailed content of How to reset the initial value of the auto-increment column in the table in MySQL and share the implementation method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn