SQL MySQL重命名数据库名称



RENAME TABLE table_name_1 TO table_name_2;

修改数据库名,不能使用 RENAME DATABASE,因为:

This statement was added in MySQL 5.1.7 but was found to be dangerous and was removed in MySQL 5.1.23. ... However, use of this statement could result in loss of database contents, which is why it was removed. Do not use RENAME DATABASE in earlier versions in which it is present.

via: https://dev.mysql.com/doc/refman/5.1/en/rename-database.html

除了使用传统的先dump再source的办法外,对于表数量较少的情况,还可以直接使用RENAME TABLE来“移表”:

RENAME TABLE DB_NAME_1.table_name_1 TO DB_NAME_2.table_name_2;

这里需要先创建 DB_NAME_2的数据库,再进行上述指令。


Tags: mysql