Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MekDrop/6d6b8b6bd9d535174ccf66bb927a0833 to your computer and use it in GitHub Desktop.
Save MekDrop/6d6b8b6bd9d535174ccf66bb927a0833 to your computer and use it in GitHub Desktop.
This generates one SQL to be used to move one database tables to other database
# Change these variables values
set @old_db='a';
set @new_db='b';
SET SESSION group_concat_max_len = 1000000;
# Don't change anything below
SELECT CONCAT('RENAME TABLE ' , GROUP_CONCAT(cmd SEPARATOR ', ')) 'sql'
FROM (
SELECT CONCAT('`',@old_db,'`.`', TABLE_NAME, '` TO `',@new_db,'`.`', TABLE_NAME, '`') cmd
FROM information_schema.tables
WHERE TABLE_SCHEMA = @old_db
) a
LIMIT 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment