Objective
Before the practice encountered a problem, in a similar pt-osc scenario, you need to swap two table names, how to ensure the foolproof?
Analysis
Some people may think, the table name is not easy to drop, rename each other.
However, what we want is to complete the table name swap at the same time, if it is sequential, may result in some data write failed, then how to do?
Solve
In fact, it's not hard to find a way from a MySQL manual: lock 2 tables at the same time, not write, and then swap the table names .
We usually lock only one table, so what should we do if we lock two tables at the same time, we can use the following method:
LOCK TABLES T1 Write, T2 write;
ALTER TABLE T1 RENAME to T3;
ALTER TABLE T2 RENAME to T1;
ALTER TABLE T3 RENAME to T2;
UNLOCK TABLES;
See, in fact, is very simple, two tables at the same time Add table-level write lock, and then change with ALTER syntax renamed on it.
The above is how the MySQL two table name swap all content, I hope this article for everyone in the use of MySQL help.