1996
MySQLERRORNotableQueryHIGH confidence

Table name not allowed in this context

Production Risk

Low — statement is rejected; no data is modified.

What this means

A table name was used in a context where it is not permitted, for example referencing an updated table in a subquery of the same UPDATE or DELETE statement without a proper derived table wrapper.

Why it happens
  1. 1Updating a table while selecting from it in the same statement without a derived table.
  2. 2Referencing the target table of a DELETE in a subquery.
How to reproduce
trigger — this will error
trigger — this will error
UPDATE t SET val = (SELECT MAX(val) FROM t);

expected output

ERROR 1996 (HY000): Table 't' is specified twice, both as a target for 'UPDATE' and as a separate source for data.

Fix

Wrap the subquery in a derived table

Wrap the subquery in a derived table
UPDATE t SET val = (SELECT max_val FROM (SELECT MAX(val) AS max_val FROM t) AS sub);

Why this works

The derived table creates a temporary snapshot, breaking the self-reference restriction.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 1996 ER_TABLENAME_NOT_ALLOWED_HERE

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors