Recargar indice AUTO_INCREMENT en MySQL


Código MySQL para reiniciar un valor AUTO_INCREMENT al máximo existente, además recargar los indices.

Muy util cuando queremos reiniciar el orden.
SET @num := 0;
UPDATE mytable SET id = @num := (@num+1);
ALTER TABLE mytable AUTO_INCREMENT = (@num);

VALIDO PARA TABLAS SIN CLAVES FORÁNEAS
VALIDO PARA TABLAS CON CLAVES FORÁNEAS EN CASCADE
NO VALIDO PARA TABLAS CON CLAVES FORÁNEAS EN RESTRICT
En línea


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.