MySQL 8.4Lab-verified · 2026-11-29

MySQL ERROR 1442 (HY000): Can't update table in stored function/trigger because it is already used by statement

A trigger tried to change the same table that the statement firing it is already changing.

MySQL 8.4.13
1442 (HY000): Can't update table 'products' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

Jump to a cause

  1. 01AFTER UPDATE trigger runs UPDATE on its own table
  2. 02AFTER INSERT trigger deletes rows from its own table

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4

In MySQL, a trigger (or stored function) cannot modify the table used by the statement that called it. An UPDATE or DELETE on its own table inside the trigger fails with ERROR 1442, and the original statement fails with it.

Below are two common patterns verified on MySQL 8.4: an AFTER UPDATE trigger that sets updated_at with another UPDATE, and an AFTER INSERT trigger that deletes older duplicate rows. The first was fixed with a BEFORE trigger that sets NEW.updated_at, the second by dropping the trigger and using a UNIQUE key with ON DUPLICATE KEY UPDATE.

01AFTER UPDATE trigger runs UPDATE on its own table

Trigger products_au runs UPDATE lab_a.products SET updated_at = NOW() WHERE id = NEW.id; after each update of products. So UPDATE products SET price = 1200 WHERE id = 1; fails with ERROR 1442 for table 'products'.

To change the row being updated, do not issue another UPDATE. We replaced it with a BEFORE UPDATE trigger that does SET NEW.updated_at = NOW();. The same UPDATE then succeeded, with price 1200 and updated_at filled in.

Setup
Setuprun as root
CREATE TABLE lab_a.products (id INT PRIMARY KEY, price INT, updated_at DATETIME);
INSERT INTO lab_a.products VALUES (1, 1000, NULL);
CREATE TRIGGER lab_a.products_au AFTER UPDATE ON lab_a.products FOR EACH ROW
  UPDATE lab_a.products SET updated_at = NOW() WHERE id = NEW.id;
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)
Reproducerun as lab_a
UPDATE products SET price = 1200 WHERE id = 1;
✕ ERROR
ERROR 1442 (HY000): Can't update table 'products' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Fixrun as root
DROP TRIGGER lab_a.products_au;
CREATE TRIGGER lab_a.products_bu BEFORE UPDATE ON lab_a.products FOR EACH ROW
  SET NEW.updated_at = NOW();
OUTPUT
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

SET NEW.col = ... works only in BEFORE triggers. In an AFTER trigger the row is already written.

After the fixrun as lab_a
UPDATE products SET price = 1200 WHERE id = 1;
SELECT id, price, updated_at FROM products;
✓ SUCCESS
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

+----+-------+---------------------+
| id | price | updated_at          |
+----+-------+---------------------+
|  1 |  1200 | 2026-11-29 22:00:14 |
+----+-------+---------------------+
1 row in set (0.00 sec)

02AFTER INSERT trigger deletes rows from its own table

Trigger cart_ai tries to remove older duplicates: after an insert it runs DELETE FROM lab_a.cart WHERE user_id = NEW.user_id AND item = NEW.item AND id < NEW.id;. Inserting (7, 'pen') fails with ERROR 1442 for table 'cart'.

There is no trigger-only way around this, so we dropped the trigger. We deleted the existing duplicate once (1 row), added UNIQUE KEY cart_user_item_uk (user_id, item), and changed the insert to INSERT ... AS new ON DUPLICATE KEY UPDATE item = new.item;. It returned 0 rows affected and the table still had one row for user 7's pen.

Setup
Setuprun as root
CREATE TABLE lab_a.cart (id INT AUTO_INCREMENT PRIMARY KEY, user_id INT, item VARCHAR(20));
INSERT INTO lab_a.cart (user_id, item) VALUES (7, 'pen'), (7, 'pen');
CREATE TRIGGER lab_a.cart_ai AFTER INSERT ON lab_a.cart FOR EACH ROW
  DELETE FROM lab_a.cart WHERE user_id = NEW.user_id AND item = NEW.item AND id < NEW.id;
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.01 sec)
Reproducerun as lab_a
INSERT INTO cart (user_id, item) VALUES (7, 'pen');
✕ ERROR
ERROR 1442 (HY000): Can't update table 'cart' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Fixrun as root
DROP TRIGGER lab_a.cart_ai;
OUTPUT
Query OK, 0 rows affected (0.01 sec)
Fixrun as lab_a
DELETE c1 FROM cart c1 JOIN cart c2
  ON c1.user_id = c2.user_id AND c1.item = c2.item AND c1.id < c2.id;
ALTER TABLE cart ADD UNIQUE KEY cart_user_item_uk (user_id, item);
OUTPUT
Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

Let a constraint prevent duplicates instead of cleaning them up in a trigger.

After the fixrun as lab_a
INSERT INTO cart (user_id, item) VALUES (7, 'pen') AS new
  ON DUPLICATE KEY UPDATE item = new.item;
SELECT id, user_id, item FROM cart;
✓ SUCCESS
Query OK, 0 rows affected (0.01 sec)

+----+---------+------+
| id | user_id | item |
+----+---------+------+
|  2 |       7 | pen  |
+----+---------+------+
1 row in set (0.00 sec)

FAQ

What does "Can't update table in stored function/trigger because it is already used by statement" mean?

The trigger (or function) tried to UPDATE, DELETE or INSERT into the same table as the statement that fired it. MySQL does not allow this, so both fail with ERROR 1442.

How do I update a column of the same row in a MySQL trigger?

Use a BEFORE trigger and assign SET NEW.column = value; instead of running UPDATE. On this page that filled updated_at without errors.

Can I delete rows from the same table in a trigger?

No. The AFTER INSERT trigger that deleted older duplicates from cart failed with ERROR 1442. Move that logic out of the trigger, for example a UNIQUE key plus ON DUPLICATE KEY UPDATE.

Does ERROR 1442 also happen with stored functions?

The message says "stored function/trigger", so the same rule applies to functions called from a statement on that table. This page tested triggers only.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13