mysqldude

Triggers

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
BEFORE UPDATE trigger writes OLD values to audit (original)
original post
✓✓✓✓✓✓✓✓✓
SET NEW.col in an AFTER trigger (expect 1362)
original post
✓ 1362✓ 1362✓ 1362✓ 1362✓ 1362✓ 1362✓ 1362✓ 1362✓ 1362
Two BEFORE UPDATE triggers on one table (FOLLOWS)
version check
✗ 1064✓✓✓✓✓✓✓✓
CREATE TRIGGER IF NOT EXISTS
version check
✗ 1064✗ 1064✓✓✓✓✓✓✓
CREATE OR REPLACE TRIGGER
version check
✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✓✓✓✓
BEFORE UPDATE trigger writes OLD values to audit (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
SET NEW.col in an AFTER trigger (expect 1362) original post
MySQL 5.6✓ 13625.7✓ 13628.0✓ 13628.4✓ 13629✓ 1362
MariaDB 10.6✓ 136210.11✓ 136211.4✓ 136211.8✓ 1362
Two BEFORE UPDATE triggers on one table (FOLLOWS) version check
MySQL 5.6✗ 10645.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
CREATE TRIGGER IF NOT EXISTS version check
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
CREATE OR REPLACE TRIGGER version check
MySQL 5.6✗ 10645.7✗ 10648.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-24 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (3)
  • Two BEFORE UPDATE triggers on one table (FOLLOWS) (MySQL 5.6): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't_a INSERT INTO audit VALUES (OLD.id, 2)' at
  • CREATE TRIGGER IF NOT EXISTS (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF NOT EXISTS t_x BEFORE INSERT ON process FO
  • CREATE OR REPLACE TRIGGER (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TRIGGER t_x BEFORE INSERT ON process FOR EACH

Related errors: 1362, 1235

The original article

First published 2010-12-12. The text below is the original; the table above shows how it behaves on current versions.

Triggers can be extremely important in keeping data consistent through the database, for auditing purposes or for massaging the data into various tables to improve performance at a later stage. In most cases this may be performed within the application directly but if someone enters directly into the database this may be bipassed resulting in inconsistencies within the data model.

I have created an example below of a trigger. An application has an internal userid integer value which is used to update a record. We can to keep an audit of the previous value. A trigger is created to handle this:

The code for the trigger is:

CREATE TRIGGER process01
BEFORE UPDATE ON process
FOR EACH ROW BEGIN
  INSERT INTO audit(id, userid) VALUES (old.id, old.userid);
END;
$$

This basically keeps track of the old values stored in the table before the UPDATE takes place. In this case the trigger may be executed either BEFORE or AFTER the UPDATE statement with the same result. However, it should be noted that if you need to update the new values to be stored then this is only permissible BEFORE UPDATE. For example:

CREATE TRIGGER process01
AFTER UPDATE ON process
FOR EACH ROW BEGIN
  INSERT INTO audit(id, userid) VALUES (old.id, old.userid);
  SET NEW.userid = 0;
END;
$$

Attempting to apply this to the database will result in:

mysql> delimiter $$
mysql> CREATE TRIGGER process01
    -> AFTER UPDATE ON process
    -> FOR EACH ROW BEGIN
    ->   INSERT INTO audit(id, userid) VALUES (old.id, old.userid);
    ->   SET NEW.userid = 0;
    -> END;
    -> $$
ERROR 1362 (HY000): Updating of NEW row is not allowed in after trigger

Now lets have a look at how this code works:

mysql> create table audit(id int, userid int);
Query OK, 0 rows affected (0.06 sec)

mysql> create table process (id int, userid int);
Query OK, 0 rows affected (0.04 sec)

mysql> delimiter $$
mysql> create trigger process_before
    -> before update on process
    -> for each row begin
    -> insert into audit(id, userid) values (old.id, old.userid);
    -> end;
    -> $$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> insert into process(id, userid) values (1,1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from process;
+------+--------+
| id   | userid |
+------+--------+
|    1 |      1 |
+------+--------+
1 row in set (0.00 sec)

mysql> select * from audit;
Empty set (0.01 sec)

mysql> update process set id = 2,userid=99;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from audit;
+------+--------+
| id   | userid |
+------+--------+
|    1 |      1 |
+------+--------+
1 row in set (0.00 sec)

mysql> update process set id = 1,userid=23;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from audit;
+------+--------+
| id   | userid |
+------+--------+
|    1 |      1 |
|    2 |     99 |
+------+--------+
2 rows in set (0.00 sec)