MySQL 8.0字符集校正

MySQL升级为8.0版本时,之前版本的字符集往往是不同的,需要校正。

执行下面的三个SQL语句的查询结果,可以从库、表、列三个层面对字符集进行校正。

  • select concat('alter database ', schema_name, ' default character set utf8mb4 collate utf8mb4_general_ci;')
      from information_schema.schemata
     where schema_name not in ('sys', 'mysql', 'performance_schema', 'information_schema')
       and (lower(default_collation_name) != 'utf8mb4_general_ci' or lower(default_character_set_name) != 'utf8mb4');
    
  • select concat('alter table ', table_schema, '.', table_name,
                  ' default character set utf8mb4 collate = utf8mb4_general_ci;')
      from information_schema.tables
     where table_schema not in ('sys', 'mysql', 'performance_schema', 'information_schema')
       and table_type = 'BASE TABLE'
       and lower(table_collation) != 'utf8mb4_general_ci';
    
  • set group_concat_max_len = 10240;
    
    select concat(c1, c2, ';')
      from (select c1, group_concat(c2) c2
              from (select concat('alter table ', t1.table_schema, '.', t1.table_name) c1,
                           concat(' modify ', '`', t1.column_name, '` ', t1.data_type,
                                  if(t1.data_type in ('varchar', 'char'), concat('(', t1.character_maximum_length, ')'),
                                     ''),
                                  if(t1.column_default != '', concat(' default \'', t1.column_default, '\''), ''),
                                  ' collate utf8mb4_general_ci',
                                  if(t1.is_nullable = 'NO', ' not null', ' null'), ' comment ', '''', t1.column_comment,
                                  '''')                                                c2
                      from information_schema.columns t1,
                           information_schema.tables t2
                     where t1.table_schema = t2.table_schema
                       and t1.table_name = t2.table_name
                       and t2.table_type = 'BASE TABLE'
                       and (lower(t1.collation_name) != 'utf8mb4_general_ci' or lower(t1.character_set_name) != 'utf8mb4')
                       and t1.table_schema not in ('sys', 'mysql', 'performance_schema', 'information_schema')) t1
             group by c1) t;
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天航星

感谢你的鼓励和认可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值