Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

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";
whisper0077
クソゲームエンジニア 雰囲気プログラミング
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした