I have a table devicedetails with ID d_deviceId as auto increment. If I delete the first ID(i.e., 0) , I need to insert the next entry to the ID value 0.
How can I implement this in MySQL.
If I delete any other ID (say 5), I use the following query:
SELECT d_deviceId + 1 FROM devicedetails d1 WHERE NOT EXISTS
(SELECT 1 FROM devicedetails d2 WHERE d2.d_deviceId = d1.d_deviceId + 1) limit 0,1
From this I get the next Reusable ID except the initial ID.
Any one suggest a way?
Stop using that query; it will give you incorrect results under high concurrency. Use the
AUTO_INCREMENTproperty on yourd_deviceIdcolumn instead — it will automatically assign the next unused value to the column every time a row is inserted.This will require that
d_deviceIdbe configured as the primary key for the table. But that should probably be the case already.http://dev.mysql.com/doc/refman/5.5/en/example-auto-increment.html