sql - how to write a mysql trigger to log history -
i have never written trigger before , following tutorial http://net.tutsplus.com/tutorials/databases/introduction-to-mysql-triggers/ confused something.
i trying write trigger pulls data table after insert , logs in history table. here's code far:
delimiter $$ create trigger trackhistory after insert on test.inventory each row begin *** end; delimiter ;
i need add insert history... query, don't understand how reference fields on correct row of 'inventory' table. answer same , update or delete?
edit: have tried follow advice of answer below, didn't work got error
1363 - there no old row in on insert trigger. whats wrong?
delimiter $$ create trigger trackinserthistory after insert on inventory each row begin insert history values (new.id, old.quantity, new.quantity, timestamp); end$$ create trigger trackstockhistory after update on inventory each row begin insert history values (new.id, old.quantity, new.quantity, timestamp); end$$ delimiter ;
they referenced new , old.
new new record inserted or updated data.
old deleted record, or old data before update.
Comments
Post a Comment