"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Renumber a Primary Index for Orderly Sequential Values in MySQL?

How to Renumber a Primary Index for Orderly Sequential Values in MySQL?

Published on 2024-11-06
Browse:516

How to Renumber a Primary Index for Orderly Sequential Values in MySQL?

Renumbering Primary Index for Orderly Sequential Values

If your MySQL table's primary index (id) appears in an inconsistent order (e.g., 1, 31, 35, 100), you may desire to rearrange them into a sequential series (1, 2, 3, 4).

To accomplish this, you can employ the following approach without creating a temporary table:

SET @i = 0;
UPDATE table_name SET column_name = (@i := @i   1);

This SQL statement initiates a counter variable @i to 0. The UPDATE operation iterates through the table_name and assigns each row's column_name column with the incremented value of the counter.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3