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

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

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

1. Problem Statement

In MySQL database design, an auto-increment numeric column is usually designed to be used as a primary key that is not related to the business. After frequent deletion or clearing operations occur in the database, its auto-increment value will still increase automatically. What should you do if you 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 take a long time, so this problem needs attention.

b. truncate

truncate table_name 

Simple and fast, clear data directly.

3. delete vs tuncate

The main differences are as follows:

  • Truncate is fast and does not record logs, so rollback is not possible. delete and vice versa.
  • Truncate will reset the index and auto-increment initial value, but delete will not.
  • Truncate will not trigger the trigger, but delete will.

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • How to reset the initial value of AUTO_INCREMENT column in mysql

<<:  Example of implementing todo application with Vue

>>:  Solution for installing opencv 3.2.0 in Ubuntu 18.04

Recommend

Example code for element multiple tables to achieve synchronous scrolling

Element UI implements multiple tables scrolling a...

How to build a redis cluster using docker

Table of contents 1. Create a redis docker base i...

How to configure common software on Linux

When you get a new Linux server, you generally ha...

Implementation of scheduled backup in Mysql5.7

1. Find mysqldump.exe in the MySQL installation p...

Recommend several MySQL related tools

Preface: With the continuous development of Inter...

Detailed explanation of three ways to set borders in HTML

Three ways to set borders in HTML border-width: 1...

Docker builds CMS on-demand system with player function

Table of contents text 1. Prepare the machine 2. ...

Hidden overhead of Unix/Linux forks

Table of contents 1. The origin of fork 2. Early ...

Form submission refresh page does not jump source code design

1. Design source code Copy code The code is as fol...

MySQL backup table operation based on Java

The core is mysqldump and Runtime The operation i...

Server stress testing concepts and methods (TPS/concurrency)

Table of contents 1 Indicators in stress testing ...

Delegating Privileges in Linux Using Sudo

Introduction to sudo authority delegation su swit...

Mysql query database capacity method steps

Query the total size of all databases Here’s how:...