ITPub博客

首页 > Linux操作系统 > Linux操作系统 > mysql rename table Syntax

mysql rename table Syntax

原创 Linux操作系统 作者:zecaro 时间:2011-05-13 17:45:28 0 删除 编辑

        原先想坚持看mysql手册,实在看不下去,犯困。所以嘛,用到的时候就一定去看看。手册能解决很多需要反复试的东西啦。我觉得速度还是很重要的。

         今天用到rename table,所以看下这一节。

         MySQL 5.1 Manual     12.1.33. RENAME TABLE Syntax

         12.1.33. RENAME TABLE Syntax

         RENAME TABLE tbl_name TO new_tbl_name
             [, tbl_name2 TO new_tbl_name2] ...

This statement renames one or more tables.

可以同时rename多个table。

The rename operation is done atomically, which means that  no other session can access any of the tables while the rename is running. For example, if you have an existing table old_table, you can create another table new_table that has the same structure but is empty, and then replace the existing table with the empty one as follows (assuming that backup_table does not already exist):

CREATE TABLE new_table (...);
RENAME TABLE old_table TO backup_table, new_table TO old_table;
上面这个我觉得挺不错的,今天也正好用到了。

If the statement renames more than one table, renaming operations are done from left to right. If you want to swap two table names, you can do so like this (assuming that tmp_table does not already exist):

多个table,rename为从左至右的顺序。

RENAME TABLE old_table TO tmp_table,
                          new_table TO old_table,
                          tmp_table TO new_table;

As long as  two databases are on the same file system, you can use  RENAME TABLE  to move a table from one database to another:

可以改到另一个database,前提是在同一个文件系统上,这个一般都会满足吧。

RENAME TABLE current_db.tbl_name TO other_db.tbl_name;

If there are any triggers associated with a table which is moved to  a different database  using  RENAME TABLE , then the statement   fails with the error Trigger in wrong schema.

有trigger,还要改database,那就会报错了,Trigger in wrong schema。

RENAME TABLE also works for views, as long as you  do not try to rename a view into a different database.

rename table 可以改变view的名字,但是不能rename到另一个数据库。

Any privileges granted specifically for the renamed table or view are not migrated to the new name. They must be changed manually.

对于table或view上特别指定的权限就不能自动转移啦,只能手动修改了。

When you execute RENAME, you cannot have any locked tables or active transactions. You must also have the  ALTER  and  DROP  privileges on the original table, and the  CREATE  and  INSERT  privileges on the new table.

rename不能被锁住表,或者有active transactions。旧表你要有ALTER and DROP权限,然后需要CREATE and INSERT来对新表进行操作

If MySQL encounters any errors in a multiple-table rename, it does a reverse rename for all renamed tables to  return everything  to its original state.

多个表rename时出错的话,所有的表都会回到原先状态。

You  cannot use RENAME  to rename a TEMPORARY  table. However, you can use  ALTER TABLE  instead:

mysql> ALTER TABLE orig_name RENAME new_name;
不能rename临时表,不过可以用alter talbe来修改。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23650854/viewspace-695239/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: mysql时间函数
下一篇: MySQL AUTO_INCREMENT
请登录后发表评论 登录
全部评论

注册时间:2010-10-28

  • 博文量
    182
  • 访问量
    391572