Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
45
Help us understand the problem. What is going on with this article?
@whisper0077

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

More than 3 years have 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";
45
Help us understand the problem. What is going on with this article?
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
whisper0077
少しだけプログラムかけます

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
45
Help us understand the problem. What is going on with this article?