SELECT
CONCAT(
'ALTER TABLE `',
TABLE_NAME,
'` MODIFY `',
COLUMN_NAME,
'` ',
DATA_TYPE,
'(',
CHARACTER_MAXIMUM_LENGTH,
') CHARACTER SET UTF8 COLLATE utf8_general_ci',
( CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END ),
';'
)
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = (select database())
AND DATA_TYPE = 'varchar'
AND ( CHARACTER_SET_NAME != 'utf8' OR COLLATION_NAME != 'utf8_general_ci' );
SELECT
CONCAT( 'ALTER TABLE ', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;' )
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = (select database());