开发者

I need to create Trigger for delete action which backsup all fields old value in audit table

I need to create Trigger for delete action which backsup all fields old value in audit table.

I have a table structure for audit table as

id, menuid, field, oldvalue, changedone

now whenever any o开发者_如何学JAVAf the row is deleted from its mother table(menu) having 21 fields in count, every fields old value should get insert in the audit table with new audit id..

like If I delete a row having fields as:

menuid, name, age, address, sex, town

now in audit table 6 rows should get inserted seperately for every field given above as:

AUdit Table:

id=2(audittable id) 
             menuid = menuid
             field = name
             oldvalue = joy
             changedone = (whatever the deleted time was)

             id=3(audittable id) 
             menuid = menuid
             field = age
             oldvalue = 23
             changedone = (whatever the deleted time was)

an so on..


Something like this should be what you're looking for.

delimiter //
CREATE TRIGGER audit_menu BEFORE DELETE ON menu
FOR EACH ROW
BEGIN
    INSERT INTO audit (menuid, field, oldvalue, changedone) VALUES
        (OLD.menuid, 'name', OLD.name, UNIX_TIMESTAMP() ),
        (OLD.menuid, 'age', OLD.age, UNIX_TIMESTAMP() ),
        (OLD.menuid, 'address', OLD.address, UNIX_TIMESTAMP() ),
        (OLD.menuid, 'sex', OLD.sex, UNIX_TIMESTAMP() ),
        (OLD.menuid, 'town', OLD.town, UNIX_TIMESTAMP() )
END;//
delimiter ;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜