views:

96

answers:

1

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 of 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..

+1  A: 

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 ;
thetaiko
WIll it do the multiple insert as u have mentioned?
OM The Eternity
As many inserts as you want. This one inserts for name, age, address, sex, town. Add more fields as you see fit.
thetaiko