Skip to content

Instantly share code, notes, and snippets.

@endrik-exe
Created November 9, 2018 04:21
Show Gist options
  • Save endrik-exe/679c46ac1fcd7831c70856a0e4629c0c to your computer and use it in GitHub Desktop.
Save endrik-exe/679c46ac1fcd7831c70856a0e4629c0c to your computer and use it in GitHub Desktop.
Duplicate Database from template, useful for restore point
SELECT @tbl_template := 'vcerp_template', @tbl_target := 'vcerp';
SELECT 'SET FOREIGN_KEY_CHECKS=0;'
UNION
SELECT Concat('DROP TABLE IF EXISTS ', @tbl_target, '.', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = @tbl_template
UNION
SELECT Concat('CREATE TABLE ', @tbl_target, '.', TABLE_NAME, ' LIKE ', @tbl_template, '.', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = @tbl_template
UNION
SELECT Concat('INSERT INTO ', @tbl_target, '.', TABLE_NAME, ' SELECT * FROM ', @tbl_template, '.', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = @tbl_template
UNION
SELECT 'SET FOREIGN_KEY_CHECKS=1;';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment