Jump to content

Deleting records and auto increment PK


liquinas

Recommended Posts

Hi, my problem is a simple one yet one that causes much trouble on my application. It's a car inventory database where the owner can add, delete, and remove records.

 

The cars on the database have ID as the primary key. There are 6 cars on the table and the PK is on auto increment so that if I add a new car right now it will be ID#7. The problem is, if I add record #7, and delete it, the auto-increment stays set, so that if I add another record, it will become ID#8 instead of 7 again.

 

Is there anything I can add to the delete query so that the auto increment goes down by 1 each time a record is deleted?

 

Is there any other possible solution to this?

 

Thanks.

Link to comment
Share on other sites

Yes, I have every intent of pimping my primary keys like Cambodian hookers. And I do think it's quite lame that mysql doesn't automatically reduce it. If this task if possible via any way at all, I'd like to at least know so I can evaluate for myself whether or not it's worth it.

Link to comment
Share on other sites

Yes, I have every intent of pimping my primary keys like Cambodian hookers. And I do think it's quite lame that mysql doesn't automatically reduce it. If this task if possible via any way at all, I'd like to at least know so I can evaluate for myself whether or not it's worth it.

 

UIDS ARE NOT COUNTERS... don't evaluate it, it's not worth it, nor should it even be allowed.  Order by whatever you want, make this column useful, and if you want to make a counter, do it in PHP.

 

MySQL doesn't do it because it's retarded.

Link to comment
Share on other sites

It seems to work without any nuclear explosions going off, but I might just be talking prematurely:

 

<?php
$user="liquinas";
$host="localhost";
$password="omglol";
$database = "tlcmotors";
$connection = mysql_connect($host,$user,$password)
	or die ("An error occured while accessing the database. Please contact liquinas@yahoo.com");
$DB = mysql_select_db($database,$connection)
	or die ("Error selecting database. Please contact liquinas@yahoo.com");
$query = "DELETE FROM cars WHERE ID='$lol'";
$result = mysql_query($query)
or die ("Problem with Query. Contact liquinas@yahoo.com");
echo "Thingy has been deleted forever and ever<br>";

?>

...

<?php
function getcarinfo()
{
$db = mysql_select_db("tlcmotors")
or die ("Error-Could not select database. Please contact liquinas@yahoo.com");
$query = "SELECT MAX(ID) FROM cars";
$result = mysql_query($query)
or die ("Error-Could not execute query. Please contact liquinas@yahoo.com");
return mysql_result($result,0,0);
}

$lmao= getcarinfo();
$rofl= $lmao-1;
?>

...

<?php
$query = "ALTER TABLE cars AUTO_INCREMENT=$rofl";
$result = mysql_query($query)
or die ("Problem with Query. Contact liquinas@yahoo.com");
echo "Have a nice day.<br>";		
?>

 

It seems to work for all intents and purposes, I'd still like to understand how it isn't a good idea?

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.