I have never written a trigger before and I am following a tutorial http://net.tutsplus.com/tutorials/databases/introduction-to-mysql-triggers/ but I am confused about something.
I am trying to write a trigger which pulls data from a table AFTER INSERT and logs it in a history table. Here's my code so far:
DELIMITER $$
CREATE TRIGGER trackHistory
AFTER INSERT ON test.inventory
FOR EACH ROW BEGIN
***
END;
DELIMITER ;
I need to add a INSERT INTO history... query, but I don't understand how I reference the fields on the correct row of the 'inventory' table. Will the answer be the same for and UPDATE or DELETE?
EDIT: I have tried to follow the advice of the answer below, but it didn't work I got this error
1363 - There is no OLD row in on INSERT trigger. Whats wrong?
DELIMITER $$
CREATE TRIGGER trackInsertHistory
AFTER INSERT ON inventory
FOR EACH ROW BEGIN
INSERT INTO history
VALUES (NEW.ID, OLD.Quantity, NEW.Quantity, TIMESTAMP);
END$$
CREATE TRIGGER trackStockHistory
AFTER UPDATE ON inventory
FOR EACH ROW BEGIN
INSERT INTO history
VALUES (NEW.ID, OLD.Quantity, NEW.Quantity, TIMESTAMP);
END$$
DELIMITER ;