MySQL批量修改字符集、排序规则(转载)
<p>版权声明:本文为CSDN<span class="name">韩悸桉</span>博主原创文章</p><p>原文链接:https://blog.csdn.net/qq_38566465/article/details/140185092</p>
<p> </p>
<h4>一、查看数据库中的字符集</h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> 查看数据库中的字符集</span>
show variables <span style="color: rgba(0, 0, 255, 1)">where</span> Variable_name <span style="color: rgba(128, 128, 128, 1)">like</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">%collation%</span><span style="color: rgba(255, 0, 0, 1)">'</span></pre>
</div>
<p>明确要统一成哪一种字符集</p>
<p><img src="https://img2024.cnblogs.com/blog/3282827/202505/3282827-20250516141208179-1094142382.png"></p>
<p> </p>
<p> </p>
<h4>二、查看某个数据库的表的用的字符集</h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> 查看某个数据库的表的用的字符集</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span> TABLE_SCHEMA <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">数据库</span><span style="color: rgba(255, 0, 0, 1)">'</span>,TABLE_NAME <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">表</span><span style="color: rgba(255, 0, 0, 1)">'</span>,TABLE_COLLATION <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">原排序规则</span><span style="color: rgba(255, 0, 0, 1)">'</span>,CONCAT(<span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">ALTER TABLE </span><span style="color: rgba(255, 0, 0, 1)">'</span>,TABLE_NAME, <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)"> CHARACTER SET=utf8mb4,COLLATE=utf8mb4_general_ci;</span><span style="color: rgba(255, 0, 0, 1)">'</span>) <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">修正SQL</span><span style="color: rgba(255, 0, 0, 1)">'</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span> information_schema.`TABLES` <span style="color: rgba(0, 0, 255, 1)">where</span> TABLE_SCHEMA <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">你的数据库名</span><span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(128, 128, 128, 1)">and</span> TABLE_COLLATION <span style="color: rgba(128, 128, 128, 1)">!=</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">utf8mb4_general_ci</span><span style="color: rgba(255, 0, 0, 1)">'</span></pre>
</div>
<p><img src="https://img2024.cnblogs.com/blog/3282827/202505/3282827-20250516141257594-2077502956.png"></p>
<p> </p>
<h4>三、查看数据库中所有字段用的排序规则</h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> 查看数据库中所有字段用的排序规则</span>
<span style="color: rgba(0, 0, 255, 1)">SELECT</span><span style="color: rgba(0, 0, 0, 1)">
TABLE_SCHEMA </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">数据库</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
TABLE_NAME </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">表</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
COLUMN_NAME </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">字段</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
DATA_TYPE </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">字段类型</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
CHARACTER_SET_NAME </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">原字符集</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
COLLATION_NAME </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">原排序规则</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
COLUMN_DEFAULT </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">列的默认值</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
CONCAT(
</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">ALTER TABLE </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
TABLE_NAME,
</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)"> MODIFY COLUMN </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
COLUMN_NAME,
</span><span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
COLUMN_TYPE,
</span><span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> - 设置新的编码和排序规则</span>
<span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)"> CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
( </span><span style="color: rgba(255, 0, 255, 1)">CASE</span> <span style="color: rgba(0, 0, 255, 1)">WHEN</span> IS_NULLABLE <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">NO</span><span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(0, 0, 255, 1)">THEN</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)"> NOT NULL</span><span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(0, 0, 255, 1)">ELSE</span> <span style="color: rgba(255, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">END</span><span style="color: rgba(0, 0, 0, 1)"> ),
( </span><span style="color: rgba(255, 0, 255, 1)">CASE</span> <span style="color: rgba(0, 0, 255, 1)">WHEN</span> COLUMN_COMMENT <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">THEN</span> <span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(0, 0, 255, 1)">ELSE</span> concat( <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)"> COMMENT</span><span style="color: rgba(255, 0, 0, 1)">'''</span>, COLUMN_COMMENT, <span style="color: rgba(255, 0, 0, 1)">''''</span> ) <span style="color: rgba(0, 0, 255, 1)">END</span><span style="color: rgba(0, 0, 0, 1)"> ),
( </span><span style="color: rgba(255, 0, 255, 1)">CASE</span> <span style="color: rgba(0, 0, 255, 1)">WHEN</span> COLUMN_DEFAULT <span style="color: rgba(0, 0, 255, 1)">is</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span> <span style="color: rgba(0, 0, 255, 1)">THEN</span> <span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(255, 0, 0, 1)">'</span> <span style="color: rgba(0, 0, 255, 1)">ELSE</span> concat(<span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">DEFAULT </span><span style="color: rgba(255, 0, 0, 1)">'''</span> , COLUMN_DEFAULT, <span style="color: rgba(255, 0, 0, 1)">''''</span>) <span style="color: rgba(0, 0, 255, 1)">END</span><span style="color: rgba(0, 0, 0, 1)"> ),
</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">;</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
) </span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">修正SQL</span><span style="color: rgba(255, 0, 0, 1)">'</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span><span style="color: rgba(0, 0, 0, 1)">
information_schema.`COLUMNS`
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span>
<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> 过滤正确排序规则</span>
COLLATION_NAME <span style="color: rgba(128, 128, 128, 1)">!=</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">utf8mb4_general_ci</span><span style="color: rgba(255, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> 数据库名称</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> TABLE_SCHEMA <span style="color: rgba(128, 128, 128, 1)">=</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">ms_db</span><span style="color: rgba(255, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> 修改varcahr和text类型的字段</span>
<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> AND (DATA_TYPE = 'varchar' OR DATA_TYPE = 'text' OR DATA_TYPE = 'longtext')</span>
</pre>
</div>
<p>上述sql可以保证在修改字符集和排序规则时,<span class="words-blog hl-git-1" data-tit="MySQL" data-pretit="mysql">MySQL字段默认值不做任何修改;</span></p>
<p>如果不保留字段默认值,做sql修改,会将修改过的字段的默认值删除掉,会导致java代码在配合数据库使用的时候一个代码正确性的问题。</p>
<p><img src="https://img2024.cnblogs.com/blog/3282827/202505/3282827-20250516141430503-1466619607.png"></p>
<p> </p>
<div class="cnblogs_code">
<pre>上面的sql可以根据自己的情况,增加where条件筛选需要操作的表。修改sql可以直接批量复制后运行,以防万一执行前请先备份相关数据</pre>
</div>
<h4>四、修改数据库的字符集及排序规则</h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">ALTER</span> <span style="color: rgba(0, 0, 255, 1)">DATABASE</span> `shtp_sunhouse` <span style="color: rgba(0, 0, 255, 1)">CHARACTER</span> <span style="color: rgba(0, 0, 255, 1)">SET</span> <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">utf8mb4</span><span style="color: rgba(255, 0, 0, 1)">'</span> COLLATE <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">utf8mb4_general_ci</span><span style="color: rgba(255, 0, 0, 1)">'</span>;</pre>
</div>
<p>存储字符集<br>utf8 是 Mysql 中的一种字符集,只支持最长三个字节的 UTF-8 字符,也就是 Unicode 中的基本多文本平面。<br>要在 Mysql 中保存 4 字节长度的 UTF-8 字符,需要使用 utf8mb4 字符集,但只有 5.5.3 版本以后的才支持。<br>为了获取更好的兼容性,应该总是使用 utf8mb4 而非 utf8. 对于 CHAR 类型数据,utf8mb4 会多消耗一些空间,根据 Mysql 官方建议,使用 VARCHAR 替代 CHAR。<br>一般现在新建数据库都是使用8.0以上的,都使用utf8mb4即可。</p>
<p>排序字符集<br>utf8mb4_unicode_ci 和 utf8mb4_general_ci<br>1、准确性</p>
<ul>
<li>utf8mb4_unicode_ci 是基于标准的 Unicode 来排序和比较,能够在各种语言之间精确排序</li>
<li>utf8mb4_general_ci 没有实现 Unicode 排序规则,在遇到某些特殊语言或者字符集,排序结果可能不一致。</li>
</ul>
<p>2、性能</p>
<ul>
<li>utf8mb4_general_ci 在比较和排序的时候更快</li>
<li>utf8mb4_unicode_ci 在特殊情况下,Unicode 排序规则为了能够处理特殊字符的情况,实现了略微复杂的排序算法。</li>
</ul>
<p>utf8mb4_0900_ai_ci<br>推荐用 utf8mb4_unicode_ci,但是用 utf8mb4_general_ci 也没啥问题。</p>
<p>MySQL 8.0 默认的是 utf8mb4_0900_ai_ci,属于 utf8mb4_unicode_ci 中的一种,具体含义如下:</p>
<p>uft8mb4 表示用 UTF-8 编码方案,每个字符最多占 4 个字节。<br>0900 指的是 Unicode 校对算法版本。(Unicode 归类算法是用于比较符合 Unicode 标准要求的两个 Unicode<br>字符串的方法)。<br>ai 指的是口音不敏感。也就是说,排序时 e,è,é,ê 和 ë 之间没有区别。<br>ci 表示不区分大小写。也就是说,排序时 p 和 P 之间没有区别。<br>默认字符集<br>utf8mb4 已成为默认字符集,在 MySQL 8.0.1 及更高版本中将 utf8mb4_0900_ai_ci 作为默认排序规则。以前,utf8mb4_general_ci 是默认排序规则。<br>由于 utf8mb4_0900_ai_ci 排序规则现在是默认排序规则,因此默认情况下新表格可以存储基本多语言平面之外的字符。现在可以默认存储表情符号。<br>如果需要重音灵敏度和区分大小写,则可以使用 utf8mb4_0900_as_cs 代替。</p><br><br>
来源:https://www.cnblogs.com/BigY/p/18879947 感谢楼主的分享!这篇帖子对于需要批量修改MySQL字符集和排序规则的童鞋来说非常实用。
现在很多项目都统一用utf8mb4了,特别是要支持表情符号存贮的时候。之前遇到过一个老项目要迁移数据库,字符集不统一导致乱码问题,头疼死了。
有个小问题想请教一下:如果数据库里数据量比较大的话,执行这些ALTER语句的时候会不会锁表比较厉害?有没有什么好的方案可以平滑迁移?
另外提醒一下各位同学,操作前一定要先备份!重要的事情说三遍:备份!备份!备份!
温馨提示:生产环境操作前建议先在测试环境验证一下SQL的准确性,确保万无一失再上线。
总之很实用,收藏了!:handshake
頁:
[1]