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

How to install MySQL via SSH on a CentOS VPS

Type yum install mysql-server Press Y to continue...

Detailed explanation of Docker container data volumes

What is Let’s first look at the concept of Docker...

Linux uses lsof command to check file opening status

Preface We all know that in Linux, "everythi...

Solution to MySql service disappearance for unknown reasons

Solution to MySql service disappearance for unkno...

Vue encapsulation component upload picture component

This article example shares the specific code of ...

About the pitfalls of implementing specified encoding in MySQL

Written in front Environment: MySQL 5.7+, MySQL d...

Implementation of built-in modules and custom modules in Node.js

1. Commonjs Commonjs is a custom module in nodejs...

How Database SQL SELECT Queries Work

As Web developers, although we are not profession...

MySQL uses frm files and ibd files to restore table data

Table of contents Introduction to frm files and i...

Create a screen recording function with JS

OBS studio is cool, but JavaScript is cooler. Now...

25 Examples of Using Circular Elements in Web Design

Today, this post lists some great examples of circ...

Analysis of implicit bug in concurrent replication of MySQL 5.7

Preface Most of our MySQL online environments use...

Install and configure MySQL under Linux

System: Ubuntu 16.04LTS 1\Download mysql-5.7.18-l...