Created
September 28, 2022 20:15
-
-
Save PureLandFlying/012b41a4f4078857c3cd137e9a8d7cd1 to your computer and use it in GitHub Desktop.
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 @database = "database name"; | |
SET @charset = "utf8mb4"; | |
SET @collate = "utf8mb4_unicode_520_ci"; | |
SELECT "SET foreign_key_checks = 0;" | |
UNION ALL | |
SELECT concat( | |
"ALTER DATABASE `", | |
`SCHEMA_NAME`, | |
"` CHARACTER SET = ", | |
@charset, | |
" COLLATE = ", | |
@collate, | |
";" | |
) AS `sql` | |
FROM `information_schema`.`SCHEMATA` | |
WHERE `SCHEMA_NAME` = @database | |
AND ( | |
`DEFAULT_CHARACTER_SET_NAME` <> @charset | |
OR `DEFAULT_COLLATION_NAME` <> @collate | |
) | |
UNION ALL | |
SELECT concat( | |
"ALTER TABLE `", | |
`TABLE_SCHEMA`, | |
"`.`", | |
`TABLE_NAME`, | |
"` CONVERT TO CHARACTER SET ", | |
@charset, | |
" COLLATE ", | |
@collate, | |
";" | |
) AS `sql` | |
FROM `information_schema`.`TABLES` | |
WHERE `TABLE_SCHEMA` = @database | |
AND `TABLE_TYPE` = "BASE TABLE" | |
AND `TABLE_COLLATION` <> @collate | |
UNION ALL | |
SELECT concat( | |
"ALTER TABLE `", | |
c.`TABLE_SCHEMA`, | |
"`.`", | |
c.`TABLE_NAME`, | |
"` CHANGE `", | |
c.`COLUMN_NAME`, | |
"` `", | |
c.`COLUMN_NAME`, | |
"` ", | |
c.`COLUMN_TYPE`, | |
" CHARACTER SET ", | |
@charset, | |
" COLLATE ", | |
@collate, | |
if(c.`IS_NULLABLE`="YES", " NULL", " NOT NULL"), | |
";" | |
) AS `sql` | |
FROM `information_schema`.`COLUMNS` c, | |
`information_schema`.`TABLES` t, | |
`information_schema`.`COLLATION_CHARACTER_SET_APPLICABILITY` a | |
WHERE c.`TABLE_SCHEMA` = t.`TABLE_SCHEMA` | |
AND c.`TABLE_NAME` = t.`TABLE_NAME` | |
AND t.`TABLE_COLLATION` = a.`COLLATION_NAME` | |
AND c.`TABLE_SCHEMA` = @database | |
AND c.`DATA_TYPE` IN ( | |
'varchar', | |
'char', | |
'text', | |
'tinytext', | |
'mediumtext', | |
'longtext' | |
) | |
AND ( | |
c.`CHARACTER_SET_NAME` <> a.`CHARACTER_SET_NAME` | |
OR c.`COLLATION_NAME` <> t.`TABLE_COLLATION` | |
) | |
AND t.`TABLE_TYPE` = "BASE TABLE" | |
UNION ALL | |
SELECT "SET foreign_key_checks = 1;"; | |
copy the result and execute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment