TRIGGERS
suggest changeSyntax
- CREATE
CREATE
[DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name
trigger_time trigger_event
ON tbl_name FOR EACH ROW
[trigger_order]
trigger_body
- trigger_time: { BEFORE | AFTER }
- trigger_event: { INSERT | UPDATE | DELETE }
- trigger_order: { FOLLOWS | PRECEDES } other_trigger_name
Remarks
Two points matter if you already use triggers on other databases:
FOR EACH ROW
FOR EACH ROW is a mandatory part of the syntax
You cannot make a statement-level trigger (once per statement) as Oracle does. MySQL triggers are row-level only.
CREATE OR REPLACE TRIGGER
The CREATE OR REPLACE is not supported by MySQL
MySQL does not allow that syntax. Drop the old trigger and create it again:
DELIMITER $$
DROP TRIGGER IF EXISTS myTrigger;
$$
CREATE TRIGGER myTrigger
-- ...
$$
DELIMITER ;
Be careful, this is not an atomic transaction :
- you lose the old trigger if the
CREATEfails - under load, other statements can run between the
DROPand theCREATE.LOCK TABLES myTable WRITEbefore the drop, andUNLOCK TABLESafter the create, blocks those writers.CREATE TRIGGERcauses an implicit commit, so this pair is still not one transaction.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents