sql >> Databasteknik >  >> RDS >> Mysql

MySQL och lås en tabell, läs och trunkera sedan

Du kan inte trunkera en tabell som är låst för skrivning. Detta beror på att "truncate" betyder "förstör tabellen och återskapa en ny med samma schema."

Du kan dock tömma bordet. Istället för TRUNCATE TABLE asin_one_time_only använd DELETE FROM asin_one_time_only . Observera att detta inte kommer att återställa autoinkrement-numreringen. Om du också vill återställa den, använd ALTER TABLE asin_one_time_only auto_increment=1

Jag föreslår att du gör så här:

LOCK TABLES asin_one_time_only READ;
SELECT asin FROM asin_one_time_only;
-- minimize the possibility of someone writing to the table in-between
-- an "UNLOCK TABLES" and a "LOCK TABLES" by just issuing a new LOCK TABLES
-- I am not 100% sure that MySQL will do this atomically, so there is a
-- possibility that you may delete a row that was not read.
-- If this is unacceptable, then use a "LOCK TABLES asin_one_time_only WRITE"
-- from the very beginning.
LOCK TABLES asin_one_time_only WRITE;
DELETE FROM asin_one_time_only;
ALTER TABLE asin_one_time_only auto_increment=1;
UNLOCK TABLES;


  1. mysql min där uttalande

  2. Kör ProxySQL som en Helper Container på Kubernetes

  3. In Flask-migrera ValueError:ogiltig interpolationssyntax i anslutningssträng vid position 15

  4. InnoDB-korruption i Xampp