MySQL Error: Can't Update Table in Stored Function/Trigger
Question:
When attempting to insert a new row into a MySQL table using a trigger, the following error occurs:
Can't update table 'brandnames' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Answer:
This error is encountered when the INSERT trigger attempts to modify the table that invoked it. MySQL prohibits this behavior for two main reasons:
To resolve this error, consider using the following approaches:
For example, to create a short name field from the first two characters of a full name, the following BEFORE INSERT trigger can be used:
CREATE TRIGGER `capital` BEFORE INSERT ON `brandnames` FOR EACH ROW BEGIN SET NEW.short_name = CONCAT(UCASE(LEFT(NEW.full_name,1)) , LCASE(SUBSTRING(NEW.full_name,2))) END
The above is the detailed content of Why Can't I Update a Table Within a MySQL Stored Function or Trigger?. For more information, please follow other related articles on the PHP Chinese website!