0

I just need a logic on how to Reset Autoincremented ID to the previous ID which I deleted recently from database for example I just delete ID=3 from database next time I add data into database ID will be incremented to 4 and it will be showed as 1 2 4 but I dont want 4 I want it to be 3 Im building a small inventory management system using php and sql.

1 Answers1

0

The following query will set your AUTO_INCREMENT value to 1 less than what it currently is, however I would recommend you don't bother doing so - recycling key values is not recommended practice and can be risky as it's not the way AUTO_INCREMENT is intended to be used (see answers to this question).

ALTER TABLE TableName AUTO_INCREMENT =
(SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'TableName') - 1;
Oliver Dalton
  • 379
  • 2
  • 16