mysqldude

Triggers for auditing

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Post's audit table + triggers, then INSERT (original as published)
original post
✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054
Post's UPDATE trigger references NEW.QUANTITY (not in table)
original post
✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054
NEW.product_id inside a BEFORE INSERT trigger is 0 for AUTO_INCREMENT
version check
✓✓✓✓✓✓✓✓✓
Corrected: own audit PK, AFTER INSERT, NULL-safe price compare
modern rewrite
✓✓✓✓✓✓✓✓✓
Post's design with only the column name fixed: audit PK = product_id
version check
✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062
Post's audit table + triggers, then INSERT (original as published) original post
MySQL 5.6✗ 10545.7✗ 10548.0✗ 10548.4✗ 10549✗ 1054
MariaDB 10.6✗ 105410.11✗ 105411.4✗ 105411.8✗ 1054
Post's UPDATE trigger references NEW.QUANTITY (not in table) original post
MySQL 5.6✗ 10545.7✗ 10548.0✗ 10548.4✗ 10549✗ 1054
MariaDB 10.6✗ 105410.11✗ 105411.4✗ 105411.8✗ 1054
NEW.product_id inside a BEFORE INSERT trigger is 0 for AUTO_INCREMENT version check
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Corrected: own audit PK, AFTER INSERT, NULL-safe price compare modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Post's design with only the column name fixed: audit PK = product_id version check
MySQL 5.6✓ 10625.7✓ 10628.0✓ 10628.4✓ 10629✓ 1062
MariaDB 10.6✓ 106210.11✓ 106211.4✓ 106211.8✓ 1062
✓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)
  • Post's audit table + triggers, then INSERT (original as published) (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1054: Unknown column 'adminuser' in 'field list'
  • Post's audit table + triggers, then INSERT (original as published) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1054: Unknown column 'adminuser' in 'INSERT INTO'
  • Post's UPDATE trigger references NEW.QUANTITY (not in table) (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9, MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1054: Unknown column 'QUANTITY' in 'NEW'

Related errors: 1054, 1062

The original article

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

Triggers are ideal for performing auditing. A trigger is database code that gets executed when either an INSERT, UPDATE or DELETE occurs on a particular table. Let’s imagine we have a pricing table which we want to make sure that keep an audit or everytime a price change happens in that table.

For this example we have a table called “products” which consists of numerous fields including “price”.

CREATE TABLE products (
  product_id INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  product_code VARCHAR(5),
  name VARCHAR(255),
  description TEXT,
  price DECIMAL(10,2)
) ENGINE = InnoDB;

We want to make sure that we keep track of the changes. The audit should consist of the product identifier, the operation performed i.e. INSERT, UPDATE or DELETE, a timestamp, the new price and a user.

As such we want our audit table to look like:

CREATE TABLE products_aud (
  product_id INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  operation CHAR(1),
  transdate DATETIME,
  price DECIMAL(10,2),
  user VARCHAR(30)
) ENGINE = InnoDB;

To refer to the trigger values in the trigger code we use NEW.field and OLD.field. In the example above, during the insert we want to access the NEW.price field, during the UPDATE we want to compare the NEW.price and the OLD.price to make sure that they are different.

In the example above we want to also include a “user” in the audit which is not included in the INSERT, UPDATE or DELETE statement. In this case here we can set a user defined variable which includes the name. This should be set somewhere after the database connection and before the insert into the table.

SET @username := 'admin';

Using this information we can now complete the code for the trigger:

DELIMITER $$

CREATE DEFINER=CURRENT_USER TRIGGER products_aud_ins
BEFORE INSERT
ON products
FOR EACH ROW
BEGIN
  INSERT INTO products_aud (product_id, transdate, operation, price, adminuser)
  VALUES (NEW.PRODUCT_ID, NOW(), 'I', NEW.PRICE, @username);
END;
$$

CREATE DEFINER=CURRENT_USER TRIGGER products_aud_upd
BEFORE UPDATE
ON products
FOR EACH ROW
BEGIN
  IF NEW.QUANTITY != OLD.QUANTITY OR NEW.PRICE != OLD.PRICE THEN
    INSERT INTO products_aud (product_id, transdate, operation, price, adminuser)
    VALUES (NEW.PRODUCT_ID, NOW(), 'U', NEW.PRICE, @username);
  END IF;
END;
$$

CREATE DEFINER=CURRENT_USER TRIGGER products_aud_del
BEFORE DELETE
ON products
FOR EACH ROW
BEGIN
  INSERT INTO products_aud (product_id, transdate, operation, price, adminuser)
  VALUES (OLD.PRODUCT_ID, NOW(), 'D', OLD.PRICE, @username);
END;
$$