Skip to content

Instantly share code, notes, and snippets.

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 andyj/5543d91d6b1c61012a4ab1832af1a978 to your computer and use it in GitHub Desktop.
Save andyj/5543d91d6b1c61012a4ab1832af1a978 to your computer and use it in GitHub Desktop.
Change all CHARACTER SETs in a Mysql database.sql
-- This pull back a list of tables which have NON-UTF8 encoded columns and creates a SQL script to fix them (and the table itself)
SELECT CONCAT("ALTER TABLE ",t.TABLE_SCHEMA,".",t.TABLE_NAME," CHARACTER SET utf8 COLLATE utf8_general_ci;",
"ALTER TABLE ",t.TABLE_SCHEMA,".",t.TABLE_NAME," CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;")
AS sql_to_copy_and_run
FROM information_schema.TABLES t
INNER JOIN information_schema.`COLUMNS` c ON t.TABLE_SCHEMA = c.TABLE_SCHEMA
WHERE
t.TABLE_SCHEMA = ? -- Replace ? with your Database/Schema name
AND t.table_type = 'BASE TABLE' AND character_set_name IS NOT NULL AND character_set_name <> 'utf8'
GROUP BY t.TABLE_SCHEMA, t.TABLE_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment