Last active
March 14, 2017 06:01
-
-
Save danielgindi/5a94d76ec1f64dc4057df0cfcfb9017e to your computer and use it in GitHub Desktop.
Rename 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
SET SESSION group_concat_max_len = 99999999999; | |
SET @old_schema_name = 'old_schema'; | |
SET @new_schema_name = 'new_schema'; | |
SELECT GROUP_CONCAT(statement SEPARATOR '') | |
FROM ( | |
SELECT 'DELIMITER $$\n\n' AS statement | |
UNION ALL | |
SELECT CONCAT('RENAME TABLE ',table_schema,'.',table_name, | |
' TO ',@new_schema_name,'.',table_name,';') as statement | |
FROM information_schema.TABLES | |
WHERE table_schema LIKE @old_schema_name | |
UNION ALL | |
SELECT CONCAT('USE ', @new_schema_name) AS statement | |
UNION ALL | |
SELECT CONCAT('CREATE TIGGER ',trigger_name,' ',action_timing,' ',event_manipulation,' ON ',event_object_table,'\n', | |
action_statement,'\n$$\n\n') as statement | |
from information_schema.triggers | |
where trigger_schema = @old_schema_name | |
UNION ALL | |
SELECT CONCAT('CREATE VIEW ',@new_schema_name,'.',`table_name`,' AS ',view_definition,' ', | |
(CASE WHEN (check_option = 'NONE') | |
THEN ('') | |
ELSE CONCAT('WITH ',check_option,' CHECK OPTION') | |
END) | |
,'\n$$\n\n') as statement | |
from information_schema.views | |
where table_schema = @old_schema_name | |
UNION ALL | |
SELECT CONCAT( | |
'CREATE ',routine_type,' ',specific_name,' ', | |
(CASE WHEN (dtd_identifier IS NULL) | |
THEN ('') | |
ELSE CONCAT('RETURNS ',dtd_identifier) | |
END) | |
,' ', | |
(CASE WHEN (routine_comment IS NULL) | |
THEN ('') | |
ELSE CONCAT('COMMENT ',QUOTE(routine_comment)) | |
END) | |
,' ', | |
(CASE WHEN (`routine_body` IS NULL) | |
THEN ('') | |
ELSE CONCAT('LANGUAGE ',QUOTE(routine_body)) | |
END) | |
,' ', | |
(CASE WHEN (`is_deterministic` = 'NO') | |
THEN ('NOT DETERMINISTIC') | |
ELSE 'DETERMINISTIC' | |
END) | |
,' ', | |
(CASE WHEN (`sql_data_access` IS NULL) | |
THEN ('') | |
ELSE sql_data_access | |
END) | |
,' ', | |
(CASE WHEN (`security_type` IS NULL) | |
THEN ('') | |
ELSE CONCAT('SQL SECURITY ',QUOTE(security_type)) | |
END) | |
,' ',routine_definition | |
,'\n$$\n\n') as statement | |
from information_schema.ROUTINES | |
UNION ALL | |
SELECT 'DELIMITER ;\n\n' AS statement | |
) statements; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should generate a giant query that renames your database with all its entities.