You can create trigger for the 3 types of events INSERT, UPDATE, DELETE
The syntax for creating a trigger is as follows
CREATE
[DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name
{ BEFORE | AFTER } { INSERT | UPDATE | DELETE }
ON tbl_name FOR EACH ROW
[{ FOLLOWS | PRECEDES } other_trigger_name]
trigger_body
You can review the documentation here: link
trigger_name
trigger_name
represents the name of the trigger.
{ BEFORE | AFTER }
Defines whether the trigger is executed before (BEFORE) or after the event (AFTER), on each row.
{ INSERT | UPDATE | DELETE }
Defines the event that the trigger will listen to
-
INSERT: Each time a row is inserted. For example with INSERT, LOAD DATA, and REPLACE.
-
UPDATE: Each time a row is updated with UPDATE.
-
DELETE: Each time a row is deleted, for example with DELETE and REPLACE.
tbl_name
tbl_name
represents the name of the table the trigger will listen to.
[{ FOLLOWS | PRECEDES } other_trigger_name]
When we have multiple triggers with the same event and the same time, FOLLOWS and PRECEDES allow us to define the order in which they will be executed.
trigger_body
trigger_body
represents the body of the trigger. If we need to execute multiple statements, we must use the BEGIN ... END
structure. In the body of the trigger we can access the data of the sentence with the alias NEW.columnname and with the alias OLD.columnname to the data of the row before executing the sentence.
Note: Cascades over foreign keys do not activate the triggers.