Question

How do I use the data that was used to trigger the "trigger" in the trigger itself?

For example, lets assume we have a user (user_id) who created a new mission (mission_id) and therefore entered a new row in a table called missions.

Now I want to create a log in a table called log by using that specific user_id and mission_id.

What is that right syntax to do that?

Was it helpful?

Solution

You access the pseudo rows OLD and NEW to do this. New contains the new data (e.g. Inserts and new Update data), and Old contains the outgoing data (Deletes and old values prior to an update).

CREATE TRIGGER t_missions
  BEFORE INSERT ON Missions
  FOR EACH ROW
BEGIN
   INSERT INTO Log(user_id, mission_id) VALUES(new.user_id, new.mission_id);
END;

SqlFiddle here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top