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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.