Translation: MariaDB rename table statement, mariadbrename
This article is a translation of the mariadb official manual: rename table.
Original article: https://mariadb.com/kb/en/library/rename-table/
Https://mariadb.com/kb/zh-cn/rename-table/ I submitted to the MariaDB official manual
Syntax
RENAME TABLE tbl_name [WAIT n | NOWAIT] TO new_tbl_name [, tbl_name2 TO new_tbl_name2] ...
Description
This statement can rename one or more tables or views without changing their permissions.
The rename operation is atomic, which means that other sessions cannot access the tables involved in the ongoing rename statement. It cannot be named as an existing table or view, but it can be achieved through tips. For example, if one table old_table exists, you can create another empty table new_table with the same structure, and then Replace the existing table with this empty table (assuming there is no backup_table table ):
CREATE TABLE new_table (...);RENAME TABLE old_table TO backup_table, new_table TO old_table;
Tab_name can be specified as db_name.tbl_name. See Identifier Qualifiers. This allows RENAME to move a table from one database to another (as long as the two databases are in the same file system ):
RENAME TABLE db1.t TO db2.t;
Note: If a trigger involving a table already exists, it cannot be moved across databases and the following error message is generated:
ERROR 1435 (HY000): Trigger in wrong schema
Views cannot be moved across databases:
ERROR 1450 (HY000): Changing schema from 'old_db' to 'new_db' is not allowed.
Rename table cannot operate temporary tables. You can use alter table:
CREATE TEMPORARY TABLE t1 (c INT);ALTER TABLE t1 RENAME TO t2;
If rename table is renamed to more than one TABLE and one of the tables fails to be named, all tables including those that have already performed the naming operation will be rolled back, because the rename operation is atomic.
Rename the table according to the order specified in the statement. In view of this, you can exchange data between two tables:
RENAME TABLE t1 TO tmp_table, t2 TO t1, tmp_table TO t2;
The WAIT/NOWAIT option is introduced from MariaDB 10.3.0 to set the lock WAIT timeout time. See my other translation: WAIT and NOWAIT.
Go back to the Linux series article outline: workshop!