liquinas Posted February 27, 2007 Share Posted February 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/ Share on other sites More sharing options...
mjlogan Posted February 27, 2007 Share Posted February 27, 2007 The usual response to this question is 'it is not advised'. Why do you want the ID to drop, what wrong with having a gap? Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-195319 Share on other sites More sharing options...
liquinas Posted February 27, 2007 Author Share Posted February 27, 2007 The front page of the site specificaly lists cars with ID 1 through 6. Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-195322 Share on other sites More sharing options...
mjlogan Posted February 27, 2007 Share Posted February 27, 2007 Could you not list the smallest six numbers, say you had cars 1,2,5,7,12,13,25,57 it would just show 1,2,5,7,12,13 and to do that in the SQL you would just order by the ID and limit to 6 Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-195323 Share on other sites More sharing options...
liquinas Posted February 27, 2007 Author Share Posted February 27, 2007 What would be the "not advised" alternative? Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-195334 Share on other sites More sharing options...
artacus Posted February 27, 2007 Share Posted February 27, 2007 As fenway would say, "You are abusing your primary keys." Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-195346 Share on other sites More sharing options...
liquinas Posted February 27, 2007 Author Share Posted February 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-195360 Share on other sites More sharing options...
artacus Posted February 27, 2007 Share Posted February 27, 2007 Ok, did I mention that I didn't think this was a good idea? ALTER TABLE `myTable` AUTO_INCREMENT =7 Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-195369 Share on other sites More sharing options...
fenway Posted February 27, 2007 Share Posted February 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-195386 Share on other sites More sharing options...
liquinas Posted March 1, 2007 Author Share Posted March 1, 2007 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 [email protected]"); $DB = mysql_select_db($database,$connection) or die ("Error selecting database. Please contact [email protected]"); $query = "DELETE FROM cars WHERE ID='$lol'"; $result = mysql_query($query) or die ("Problem with Query. Contact [email protected]"); 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 [email protected]"); $query = "SELECT MAX(ID) FROM cars"; $result = mysql_query($query) or die ("Error-Could not execute query. Please contact [email protected]"); 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 [email protected]"); 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? Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-197336 Share on other sites More sharing options...
bwochinski Posted March 1, 2007 Share Posted March 1, 2007 And how does it handle when someone deletes the car with an ID of 2? Also, your site lists the 6 oldest listings on the front page? Seems like you'd want the 6 most recent. Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-197374 Share on other sites More sharing options...
fenway Posted March 2, 2007 Share Posted March 2, 2007 There's nothing wrong with that script... because it doesn't play around with the UIDs. Quote Link to comment https://forums.phpfreaks.com/topic/40355-deleting-records-and-auto-increment-pk/#findComment-198059 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.