Reset autoincrement value in mysql -


i have table having serial no. 1,2,3,4,5 , on if delete data of ex. 2 no. table shows 1, 3,4,5,6.... want should reset serial no field. can me in this?

pravin

you can renumber rows of table recreating it. that's foolproof way if table small ... 100k rows or smaller.

something trick.

 rename mytable mytable_previous;  create mytable mytable_previous;  truncate table mytable;  alter table mytable auto_increment = 1;   insert mytable          (col, col, col) /* name cols except autoincrementing */  select (col, col, col) /* same names, same order */   order id; 

this allow insert operation redo autoincrementing in same order previous table, without gaps.

beware ... bad idea. if autoincrementing id column used in other tables reference table, doing wreck database. production database tables have delete operations ordinarily live gaps in autoincremented row numbers. gaps cause no harm.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -