Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last deleted ID in MySQL

Similar to LAST_INSERT_ID(), is there a nice mechanism in MySQL to get the last deleted ID after a row has been deleted?

like image 282
pillarOfLight Avatar asked Dec 20 '11 23:12

pillarOfLight


2 Answers

By "ID", I assume you mean "auto-increment"?

Since you can delete any arbitrary row (or set of rows) at any time: no, there's no way to tell WHICH row (or rows) you most recently deleted.

You can, however, create a "trigger" to save this information for you:

http://dev.mysql.com/doc/refman/5.0/en/triggers.html

like image 187
paulsm4 Avatar answered Sep 24 '22 20:09

paulsm4


Instead of creating a trigger, you need to use this each and every time when you delete

declare @table1 table(id int identity,name varchar(50))
     insert into @table1 (name)  values('abc')
     insert into @table1 (name)  values('xyz')
      insert into @table1 (name) values('pqr')
       insert into @table1 (name)  values('wqe')
     delete from  @table1 output deleted.name,deleted.id where  id=3
like image 41
Tamkeen Avatar answered Sep 24 '22 20:09

Tamkeen