I'm trying to clear / empty / truncate a table in MySQL before inserting a new set of data. I think this can only be done using a trigger.
I created the following trigger:
create trigger top_destinations_truncate BEFORE INSERT
on top_destinations FOR EACH ROW
delete from top_destinations
But when I try to add a couple hundred records with my query I get this error:
HY000Can't update table 'top_destinations' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
The insert into table top_destinations
query itself works fine if the trigger doesn't exist. But I need a fresh start every time...
What can I do? Maybe there is a better way in MySQL without a trigger: like a sequential type of query. First truncate, wait a little, then insert, if possible.
You can use a TRANSACTION, so your Table is never empty for other queries but this is not the fastest way.
START TRANSACTION;
DELETE FROM your_table;
INSERT into your_table ...
...
INSERT into your_table ....
COMMIT;
or the faster way:
TRUNCATE yout_table;
INSERT into your_table ...
...
INSERT into your_table ....
in this way the TRUNCATE deletes and recreate the table and cant be used in a TRANSACTION, but is really fast.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.