Jump to content

Reset row "id's" after arbitrary value deleted


xander85

Recommended Posts

Is there a way to reset the auto_increment value and all subsequent values for that table if you delete an arbitrary row?

example:

 

row 0 - test info

row 1 - test info

row 2 - test info

row 3 - test info

 

new auto_increment value = 4

 

I delete row 2, so table looks like:

row 0 - test info

row 1 - test info

row 3 - test info

 

the auto_increment is still 4, but there are only 3 rows in the table and the values are not correct. Is there a function or easy way to "reorganize" the row values and reset the auto_increment?

 

 

Right now I'm using this:

 

$sql = "DELETE FROM mileage WHERE mileid = '$mileid' LIMIT 1";
  $result = mysql_query($sql);
  $idquery = mysql_query("SELECT MAX(mileid) AS new_id from mileage");
  $newid=mysql_fetch_array($idquery);
  $newmileid = $newid["new_id"]+1;
  $sql2 = "ALTER TABLE mileage AUTO_INCREMENT=$newmileid";
  $result2 = mysql_query($sql2);

 

But this will only work if you are deleting the last value in the table. If you delete an arbitrary value it will not work correctly.

I didn't realize you meant a database table, I thought you were printing them in an html table.

You wouldn't want to change the IDs, that messes up the integrity of the table. It would make the ID a meaningless number if it kept changing.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.