Build Your Own DatabaseDriven Website Using PHP amp;amp; MySQL [Electronic resources]

Kevin Yank

نسخه متنی -صفحه : 190/ 112
نمايش فراداده

RENAME TABLE

RENAME TABLE tbl_name TO new_table_name[, tbl_name2 TO ..., ...]

This query quickly and conveniently renames one or more tables. This differs from ALTER TABLE tbl_name RENAME in that all the tables being renamed in the query are locked for the duration of the query, so that no other connected clients may access them. As the MySQL Reference Manual explains, this assurance of atomicity lets you replace a table with an empty equivalent, for example, if you wanted to start a new table once a certain number of entries was reached, safely:

CREATE TABLE new_table (...)
RENAME TABLE old_table TO backup_table, new_table TO old_table;

You can also move a table from one database to another by specifying the table name as db_name.tbl_name, as long as both tables are stored on the same physical disk, which is usually the case.

You must have ALTER and DROP privileges on the original table, as well as CREATE and INSERT privileges on the new table to perform this query. A RENAME TABLE query that fails to complete halfway through will be automatically reversed, so that the original state is restored.