Skip to content

Instantly share code, notes, and snippets.

@heylastway
Last active October 31, 2019 14:55
Show Gist options
  • Save heylastway/58d863605d5682391f3b535dc81f8eef to your computer and use it in GitHub Desktop.
Save heylastway/58d863605d5682391f3b535dc81f8eef to your computer and use it in GitHub Desktop.
How to convert all Database Tables and Columns to a specific Collation.
# 1.Convert the Collation of a Database
ALTER DATABASE DBNAME CHARACTER SET utf8 COLLATE utf8_general_ci;
# 2.Convert the Collation of all Tables
SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME,' COLLATE utf8_general_ci;')
FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='DBNAME' AND TABLE_TYPE = 'BASE TABLE';
# 3.Convert the Collation of Table Columns
SELECT CONCAT('ALTER TABLE `', TABLE_NAME,'` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;') AS mySQL
FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA= "DBNAME" AND TABLE_TYPE="BASE TABLE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment