How to rename two tables in one atomic operation in MySQL

我怕爱的太早我们不能终老 提交于 2021-02-04 13:07:35

问题


I need to rename two tables in one atomic operation so that user will never be able to see the database in its intermediate state.

I'm using MySQL and noticed that this case is perfectly described in the documentation:

13.3.3 Statements That Cause an Implicit Commit

The statements listed in this section (and any synonyms for them) implicitly end any transaction active in the current session, as if you had done a COMMIT before executing the statement

[...]

Data definition language (DDL) statements that define or modify database objects. ALTER DATABASE ... UPGRADE DATA DIRECTORY NAME, ALTER EVENT, ALTER PROCEDURE, ALTER SERVER, ALTER TABLE, ALTER VIEW, CREATE DATABASE, CREATE EVENT, CREATE INDEX, CREATE PROCEDURE, CREATE SERVER, CREATE TABLE, CREATE TRIGGER, CREATE VIEW, DROP DATABASE, DROP EVENT, DROP INDEX, DROP PROCEDURE, DROP SERVER, DROP TABLE, DROP TRIGGER, DROP VIEW, INSTALL PLUGIN (as of MySQL 5.7.6), RENAME TABLE, TRUNCATE TABLE, UNINSTALL PLUGIN (as of MySQL 5.7.6).

But maybe there's some kind of workaround or something like this?

My situation looks like this:

  • I have a current data set in the table named current
  • I gathered a new data set in the table named next
  • I need to rename the current table to the current_%current_date_time% and the next table to the current in one atomic operation

回答1:


Well, easy...

RENAME TABLE current TO current_20151221, next TO current;

as is stated in the manual. There it says that it's an atomic operation. Just to clear this up, implicit commits have nothing to do with it. That's a different story. That just says, that those statements end an open transaction.



来源:https://stackoverflow.com/questions/34391759/how-to-rename-two-tables-in-one-atomic-operation-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!