Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto increment after delete from a table [duplicate]

Possible Duplicate:
MySQL: Reorder/Reset auto increment primary key?

if I delete from my table a ligne and in this table i have an auto increment ID , DELETE From table where id=2

after this the table will be like this

 ID   | NAme
1        aaa
3        cccc

how can i fix the ID to be like this

ID   | NAme
1        aaa
2        cccc
3        dddd  
like image 827
Dilllllo Avatar asked Jun 26 '11 19:06

Dilllllo


People also ask

How do you adjust the auto increment in SQL After deleting some records from the table?

Reset the auto increment fieldALTER TABLE `table` AUTO_INCREMENT = number; Replacing 'number' with the result of the previous command plus one and replacing table with the table name. If you deleted all the rows in the table, then you could run the alter table command and reset it to 0.

How do you prevent the auto increment being reset when you delete all the rows of a table?

You use TRANCATE table to empty the table. TRUNCATE not only deletes the rows but resets the auto increment value by design. Use DELETE FROM table instead.

Does truncate table reset auto increment?

The TRUNCATE TABLE statement in MySQL completely deletes the table's data without removing a table's structure and always resets the auto-increment column value to zero.

How do I make auto increment start from 1 again?

ALTER TABLE suppliers AUTO_INCREMENT = 1; This example would change the next value in the AUTO_INCREMENT field (ie: next value in the sequence) to 1 for the supplier_id column in the suppliers table.


1 Answers

Look at this answer: Reorder / reset auto increment primary key

like image 96
Sebastiano Merlino Avatar answered Sep 28 '22 08:09

Sebastiano Merlino