mysqldude

Update table with new id values

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
UPDATE emp SET empid = @empid := @empid + 1 (original)
original post
✓✓⚠ 1287⚠ 1287⚠ 1287✓✓✓✓
UPDATE ... JOIN (ROW_NUMBER() OVER ...) (MySQL 8.0+, MariaDB 10.2+)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
UPDATE emp SET empid = @empid := @empid + 1 (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 (ROW_NUMBER() OVER ...) (MySQL 8.0+, MariaDB 10.2+) 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-25 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 (2)
  • UPDATE emp SET empid = @empid := @empid + 1 (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 (ROW_NUMBER() OVER ...) (MySQL 8.0+, MariaDB 10.2+) (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 empid) AS rn FROM emp) r ON e.empid

Related errors: 1287

The original article

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

MySQL offers some powerful options with user defined variables. In this post we are going to show how to renumber your unique identifier in a table. Please be aware that if these unique identified values are used elsewhere you have to also perform updates elsewhere which can be complex and is not supported by the operation being shown here. Here we are simply showing the power of user defined variables.

If this is something that you need please do not hesitate in contacting us on data migration or data manipulation operations. Consider a table emp with empid as the primary unique identifier and we need to reset these values back and incrementing from 1.

mysql> select * from emp;
+-------+-------+--------+
| empid | name  | deptid |
+-------+-------+--------+
|    49 | Tom   |      1 |
|    50 | Dick  |      2 |
|    51 | Harry |   NULL |
+-------+-------+--------+
3 rows in set (0.00 sec)

We first need to set the user defined variable to @empid = 0 and then update while at the same time performing an update to the value of the user defined variable @empid as follows:

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

mysql> update emp set empid = @empid:=@empid+1;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from emp;
+-------+-------+--------+
| empid | name  | deptid |
+-------+-------+--------+
|     1 | Tom   |      1 |
|     2 | Dick  |      2 |
|     3 | Harry |   NULL |
+-------+-------+--------+
3 rows in set (0.00 sec)