Skip to content

Instantly share code, notes, and snippets.

@flipace
Last active February 27, 2019 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flipace/667a628070487a22debde917f9813942 to your computer and use it in GitHub Desktop.
Save flipace/667a628070487a22debde917f9813942 to your computer and use it in GitHub Desktop.
MySQL Cross-Database Commands

Generate UNION ALL queries for e.g create view

SELECT CONCAT('SELECT `type` COLLATE utf8mb4_unicode_ci as type, "', schema_name, '" COLLATE utf8mb4_unicode_ci as tenant FROM `', 
    schema_name, '`.`micro_sessions` UNION ALL')
FROM information_schema.schemata
WHERE schema_name NOT IN ('information_schema','mysql','performance_schema');

Convert everything to different Charset + Collation

SELECT CONCAT(
	'ALTER TABLE `', 
	TABLE_NAME,
	'` CONVERT TO CHARACTER SET utf8mb4',
	' COLLATE utf8mb4_unicode_ci;')
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA IN (
	SELECT schema_name 
	FROM information_schema.schemata 
	WHERE schema_name NOT IN ('information_schema','mysql','performance_schema')
) AND TABLE_TYPE="BASE TABLE";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment