简体   繁体   中英

Log delete rows in certain tables | backup deleted data

I want to log deleted data from certain tables. That from logs I could recover the deleted data. Mysql supports something similar out of the box? Ideally, get a sql file with "insert".

There is a partial solution. For every table you want to audit, create BEFORE DELETE trigger and an audit table. See code below.

Keep in mind that triggers work only for DELETE FROM command. TRUNCATE bypass triggers and your deletes will not be audited.

DELIMITER //

CREATE TRIGGER table1_before_delete
BEFORE DELETE
   ON table1 FOR EACH ROW

BEGIN

   -- Insert record into audit table
   INSERT INTO table1_audit ( table1_id, deleted_date) VALUES ( OLD.id, NOW());

END; //

DELIMITER ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM