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

Detailed explanation of primary keys and transactions in MySQL

Table of contents 1. Comments on MySQL primary ke...

Nginx operation and maintenance domain name verification method example

When configuring the interface domain name, each ...

nginx proxy_cache batch cache clearing script introduction

Preface: I used the official nginx proxy_cache as...

JS implements dragging the progress bar to change the transparency of elements

What I want to share today is to use native JS to...

How to introduce pictures more elegantly in Vue pages

Table of contents Error demonstration By computed...

Detailed explanation of the principle and function of JavaScript closure

Table of contents Introduction Uses of closures C...

How to use Vue to implement CSS transitions and animations

Table of contents 1. The difference between trans...

How to set the style of ordered and unordered list items in CSS

In an unordered list ul>li, the symbol of an u...

Detailed introduction to linux host name configuration

Table of contents 1. Configure Linux hostname Con...

Solution - BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: Insufficient permissions

1) Enter the folder path where the jdk file is st...

MySQL 5.7.33 installation process detailed illustration

Table of contents Installation package download I...

Research on Web Page Size

<br />According to statistics, the average s...

Pure CSS3 realizes the effect of div entering and exiting in order

This article mainly introduces the effect of div ...