74
48

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQLでdumpせずに "RENAME DATABASE" を実現する

Last updated at Posted at 2016-07-11

その危険すぎる機能ゆえに、存在を消された幻のコマンド

RENAME DATABASE

でも、どうしてもデータベース名変えたい!っていうケース、あると思います

dumpするやり方

CREATE DATABASE new_db;

して

mysqldump ... -d old_db > old_db.dump

からの

mysql ... -d new_db < old_db.dump

とどめに

DROP DATABASE old_db;

で、実現できます。
が、DBが巨大であればあるほど時間かかりますし、現実的ではなくなります

dumpしないやり方

CREATE DATABASE new_db;

して

RENAME TABLE old_db.table_name TO new_db.table_name

を、全テーブル繰り返す
です!

RENAME ... TO ...は以下のsqlで作成できます
(SHOW TABLESとかで取ってきてもよし)

SELECT DISTINCT CONCAT(
'RENAME TABLE ', 
t.table_schema,'.', t.table_name, 
' TO ', 
"new_db", '.', t.table_name, 
';' ) 
as rename_table
FROM information_schema.tables as t WHERE t.table_schema="old_db";
74
48
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
74
48

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?