ALTER SCHEMA `db` DEFAULTCOLLATE utf8mb4_general_ci ; SELECT CONCAT('alter table ',table_name,' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;') FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA='库名'and table_type='BASE TABLE'into outfile '/data/altertable.sql';
这个语句可以生成语句,然后在mysql中执行即可
1
mysql -u用户名 -p表名 实力名称 < /data/altertable.sql
字段类型的修改可以参考下方语句
1 2 3 4
SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY `', column_name, '` ', DATA_TYPE, '(', CHARACTER_MAXIMUM_LENGTH, ') CHARACTER SET UTF8 COLLATE utf8_general_ci', (CASEWHEN IS_NULLABLE ='NO'THEN' NOT NULL'ELSE''END), ';')FROM information_schema.COLUMNS WHERE TABLE_SCHEMA ='库名'AND DATA_TYPE ='varchar'AND ( CHARACTER_SET_NAME !='utf8' OR COLLATION_NAME !='utf8_general_ci') ;