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

A brief discussion on the Linux kernel's support for floating-point operations

Currently, most CPUs support floating-point units...

Detailed tutorial on running multiple Springboot with Docker

Docker runs multiple Springboot First: Port mappi...

How to use CSS to achieve data hotspot effect

The effect is as follows: analyze 1. Here you can...

How to explain TypeScript generics in a simple way

Table of contents Overview What are Generics Buil...

MySQL replication table details and example code

MySQL replication table detailed explanation If w...

Detailed use of Echarts in vue2 vue3

Table of contents 1. Installation 2. Use Echarts ...

How to generate a unique server-id in MySQL

Preface We all know that MySQL uses server-id to ...

How to get datetime data in mysql, followed by .0

The data type of MySQL is datetime. The data stor...

Analysis of the operating principle and implementation process of Docker Hub

Similar to the code hosting service provided by G...

Example code of how to create a collapsed header effect using only CSS

Collapsed headers are a great solution for displa...

Summary of commonly used escape characters in HTML

The commonly used escape characters in HTML are s...

Using cursor loop to read temporary table in Mysql stored procedure

cursor A cursor is a method used to view or proce...