Home  >  Article  >  Database  >  How to Renumber a Primary Index for Orderly Sequential Values in MySQL?

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

Patricia Arquette
Patricia ArquetteOriginal
2024-10-24 19:30:04455browse

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:

<code class="sql">SET @i = 0;
UPDATE table_name SET column_name = (@i := @i + 1);</code>

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.

The above is the detailed content of How to Renumber a Primary Index for Orderly Sequential Values in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn