How to swap Mysql's two table names with _mysql

Source: Internet
Author: User
Tags mysql manual one table

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.