Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log when a row is deleted from a given table in MySQL

Tags:

mysql

I have a MySQL-table that's accessed from different APIs. I would like to log when any rows are deleted from this table. Is this possible?

like image 318
Fredrik Avatar asked Oct 07 '13 10:10

Fredrik


2 Answers

Yes, you can use a simple trigger, something like;

CREATE TRIGGER data_delete BEFORE DELETE ON data FOR EACH ROW
BEGIN
  INSERT INTO log(logmessage) VALUES (CONCAT('Delete of value ', OLD.id));
END

A simple SQLfiddle demo.

like image 191
Joachim Isaksson Avatar answered Oct 29 '22 11:10

Joachim Isaksson


Yes it is possible. You have to use trigger for this purpose. Using triggers you can save the data, time and other information after the delete occurs.

like image 37
Jhanvi Avatar answered Oct 29 '22 09:10

Jhanvi