MySQL ID starts to increase from 1 to quickly solve the problem of discontinuous ID

MySQL ID starts to increase from 1 to quickly solve the problem of discontinuous ID

mysql id starts from 1 and increases automatically to solve the problem of discontinuous id

As a person with obsessive-compulsive disorder, I cannot tolerate the problem of discontinuous ids after deleting some rows in the table. At first, I used

TRUNCATE TABLE tablename

To achieve the self-increment of id from the beginning, but this command will also clear the entire table, which is really a pitfall.

Later I found the correct approach:

alter table tablename auto_increment = 1;

This command will not change the content and order of the existing table. At the same time, the id of the newly inserted row will first use the deleted id to perfectly fill the vacant id.

MySQL auto-increment id jump solution (without deleting data)

Problem description:

I'm importing a large amount of data from Excel to MySQL. The auto-increment ids are not incremented in sequence, and many are skipped in the middle. I don't want to re-import them because of the huge amount.

Solution:

Sort by id or time from small to large ==> Get the row number ==> Replace the original id with the row number

SQL statement:

update tab as t1 join (select id,(@rowno:=@rowno+1) as rowno from tab a,(select (@rowno:=0)) b order by a.id) as t2 SET t1.id=t2.rowno WHERE t1.id=t2.id;

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Mysql auto-increment primary key id is not processed in this way
  • The difference and advantages and disadvantages of Mysql primary key UUID and auto-increment primary key
  • Detailed explanation of seven methods of returning auto-increment ID after inserting data in MySQL
  • Solution to running out of MySQL's auto-increment ID (primary key)
  • Mysql join table and id auto-increment example analysis
  • What you need to know about MySQL auto-increment ID
  • MySQL table auto-increment id overflow fault review solution
  • Summary of some small issues about MySQL auto-increment ID

<<:  Several techniques for playing sounds with CSS

>>:  Explore how an LED can get you started with the Linux kernel

Recommend

Vue3 (III) Website Homepage Layout Development

Table of contents 1. Introduction 2. Actual Cases...

Detailed example of reading speed of js objects

1. Accessing literals and local variables is the ...

MySQL gets the current date and time function

Get the current date + time (date + time) functio...

Installation tutorial of MySQL 5.7 green version under windows2008 64-bit system

Preface This article introduces the installation ...

A brief discussion on several specifications of JS front-end modularization

Table of contents Preface The value of front-end ...

Example tutorial on using the sum function in MySQL

Introduction Today I will share the use of the su...

Detailed tutorial on running selenium+chromedriver on the server

1. Introduction I want to use selenium to scrape ...

How to manually install MySQL 5.7 on CentOS 7.4

MySQL database is widely used, especially for JAV...

Detailed description of the use of advanced configuration of Firewalld in Linux

IP masquerading and port forwarding Firewalld sup...

WeChat Mini Programs Achieve Seamless Scrolling

This article example shares the specific code for...

Usage of if judgment in HTML

In the process of Django web development, when wr...

VSCode configuration Git method steps

Git is integrated in vscode, and many operations ...

3 ways to create JavaScript objects

Table of contents 1. Object literals 2. The new k...