I am trying to reset the auto increment value in one of my tables based on the number of rows currently in it. Here is the code I have so far.
SET @numrows = 0; SELECT COUNT(*) total, @numrows := COUNT(*) + 1 numrows FROM maj_user ; ALTER TABLE `maj_user` AUTO_INCREMENT = @numrows ; This works great if I execute it in MySQL Workbench. However, I need to save this as an SQL file and execute it as part of a database import script. If I do this, I get this:
ERROR 1064 (42000) at line 39: 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 '@numrows' at line 1 Line 39 is the ALTER TABLE statement. Any ideas?
AUTO_INCREMENTvalue toIFNULL(MAX(id),0)+1or whatever your auto-incrementing column is? By fluke it might be the same as the number of rows, but this is by no means reliable.AUTO_INCREMENTending up in the wrong state, then?