mysqldude

Renumber field values

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
UPDATE ... SET id = @x:=@x+1 ORDER BY id (original)
original post
✓✓⚠ 1287⚠ 1287⚠ 1287✓✓✓✓
UPDATE ... SET id = @x:=@x+1 ORDER BY name (original)
original post
✓✓⚠ 1287⚠ 1287⚠ 1287✓✓✓✓
UPDATE ... JOIN (999 + ROW_NUMBER() OVER (ORDER BY id)) (modern)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
UPDATE ... SET id = @x:=@x+1 ORDER BY id (original) original post
MySQL 5.6✓5.7✓8.0⚠ 12878.4⚠ 12879⚠ 1287
MariaDB 10.6✓10.11✓11.4✓11.8✓
UPDATE ... SET id = @x:=@x+1 ORDER BY name (original) original post
MySQL 5.6✓5.7✓8.0⚠ 12878.4⚠ 12879⚠ 1287
MariaDB 10.6✓10.11✓11.4✓11.8✓
UPDATE ... JOIN (999 + ROW_NUMBER() OVER (ORDER BY id)) (modern) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-23 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (3)
  • UPDATE ... SET id = @x:=@x+1 ORDER BY id (original) (MySQL 8.0, MySQL 8.4, MySQL 9): warning 1287: Setting user variables within expressions is deprecated and will be removed in a future release. Consider alternatives: 'SET variable=expres
  • UPDATE ... SET id = @x:=@x+1 ORDER BY name (original) (MySQL 8.0, MySQL 8.4, MySQL 9): warning 1287: Setting user variables within expressions is deprecated and will be removed in a future release. Consider alternatives: 'SET variable=expres
  • UPDATE ... JOIN (999 + ROW_NUMBER() OVER (ORDER BY id)) (modern) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(ORDER BY id) AS n FROM renumber) x ON x.id =

Related errors: 1287

The original article

First published 2011-04-19. The text below is the original; the table above shows how it behaves on current versions.

This post is to provide some basic techniques for renumbering values in a table. Before continuing it should be noted that if you do not FULLY understand your database data model and the implications of renumbering values this should not be attempted as there may be dependencies between the field being renumbered and other tables.

Consider the following table:

mysql> select * from renumber;
+------+------------+
| id   | name       |
+------+------------+
|    1 | Tom        |
|    2 | Dick       |
|    3 | Harry      |
|   10 | Donald     |
|   15 | Scooby Doo |
|   21 | Gerry      |
+------+------------+
6 rows in set (0.00 sec)

We need to renumber these sequentially starting with a value of 1000. In other words the record Tom will have an id of 1000, Dick will have 1001 and so on. We are to retain the current order. In order for this to work we will user defined session variables.

During the UPDATE we are auto incrementing the user defined session variable.

mysql> set @x = 999;
Query OK, 0 rows affected (0.00 sec)

mysql> update renumber set id = @x:=@x+1 order by id;
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6  Changed: 6  Warnings: 0

mysql> select * from renumber;
+------+------------+
| id   | name       |
+------+------------+
| 1000 | Tom        |
| 1001 | Dick       |
| 1002 | Harry      |
| 1003 | Donald     |
| 1004 | Scooby Doo |
| 1005 | Gerry      |
+------+------------+
6 rows in set (0.00 sec)

This could easily have been performed using a different order:

mysql> select * from renumber;
+------+------------+
| id   | name       |
+------+------------+
|    1 | Tom        |
|    2 | Dick       |
|    3 | Harry      |
|   10 | Donald     |
|   15 | Scooby Doo |
|   21 | Gerry      |
+------+------------+
6 rows in set (0.00 sec)

mysql> set @x = 999;
Query OK, 0 rows affected (0.00 sec)

mysql> update renumber set id = @x:=@x+1 order by name;
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6  Changed: 6  Warnings: 0

mysql> select * from renumber;
+------+------------+
| id   | name       |
+------+------------+
| 1005 | Tom        |
| 1000 | Dick       |
| 1003 | Harry      |
| 1001 | Donald     |
| 1004 | Scooby Doo |
| 1002 | Gerry      |
+------+------------+
6 rows in set (0.00 sec)