Created
December 14, 2017 23:55
-
-
Save woowa-hsw0/2d6bc58cd62773d0443f2401fe00f6e3 to your computer and use it in GitHub Desktop.
RENAME DATABASE / COPY DATABASE in MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT @src_schema := 'dbname', @dst_schema := 'dbname_bak'; | |
SELECT concat( | |
'CREATE TABLE ', | |
'`', @dst_schema, '`', '.', | |
'`', TABLE_NAME, '`', ' LIKE ', | |
'`', @src_schema, '`', '.', | |
'`', TABLE_NAME, '`', ';' | |
) q | |
FROM information_schema.tables | |
WHERE table_schema = @src_schema; | |
SELECT concat( | |
'INSERT INTO ', | |
'`', @dst_schema, '`', '.', | |
'`', TABLE_NAME, '`', ' ' | |
'SELECT * FROM ', | |
'`', @src_schema, '`', '.', | |
'`', TABLE_NAME, '`', ';' | |
) q | |
FROM information_schema.tables | |
WHERE table_schema = @src_schema; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT @src_schema := 'dbname', @dst_schema := 'dbname2'; | |
SELECT concat( | |
'RENAME TABLE ', | |
'`', @src_schema, '`', '.', | |
'`', TABLE_NAME, '`', ' TO ', | |
'`', @dst_schema, '`', '.', | |
'`', TABLE_NAME, '`', ';' | |
) q | |
FROM information_schema.tables | |
WHERE table_schema = @src_schema; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment