<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=MySQL_%E5%AD%97%E7%AC%A6%E9%9B%86%EF%BC%9A%E5%88%97%E5%AD%97%E7%AC%A6%E9%9B%86%E8%BD%AC%E6%8D%A2</id>
	<title>MySQL 字符集：列字符集转换 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=MySQL_%E5%AD%97%E7%AC%A6%E9%9B%86%EF%BC%9A%E5%88%97%E5%AD%97%E7%AC%A6%E9%9B%86%E8%BD%AC%E6%8D%A2"/>
	<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=MySQL_%E5%AD%97%E7%AC%A6%E9%9B%86%EF%BC%9A%E5%88%97%E5%AD%97%E7%AC%A6%E9%9B%86%E8%BD%AC%E6%8D%A2&amp;action=history"/>
	<updated>2026-05-02T09:21:49Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=MySQL_%E5%AD%97%E7%AC%A6%E9%9B%86%EF%BC%9A%E5%88%97%E5%AD%97%E7%AC%A6%E9%9B%86%E8%BD%AC%E6%8D%A2&amp;diff=6507&amp;oldid=prev</id>
		<title>2023年3月31日 (五) 15:04 Eijux</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=MySQL_%E5%AD%97%E7%AC%A6%E9%9B%86%EF%BC%9A%E5%88%97%E5%AD%97%E7%AC%A6%E9%9B%86%E8%BD%AC%E6%8D%A2&amp;diff=6507&amp;oldid=prev"/>
		<updated>2023-03-31T15:04:34Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2023年3月31日 (五) 23:04的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;第1行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第1行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;MySQL&lt;/del&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;MySQL文档&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;要将二进制或非二进制字符串列转换为使用特定字符集，请使用“'''ALTER TABLE'''”。为了成功进行转换，必须满足以下条件之一：&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;要将二进制或非二进制字符串列转换为使用特定字符集，请使用“'''ALTER TABLE'''”。为了成功进行转换，必须满足以下条件之一：&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=MySQL_%E5%AD%97%E7%AC%A6%E9%9B%86%EF%BC%9A%E5%88%97%E5%AD%97%E7%AC%A6%E9%9B%86%E8%BD%AC%E6%8D%A2&amp;diff=2172&amp;oldid=prev</id>
		<title>Eijux：​建立内容为“category:MySQL  要将二进制或非二进制字符串列转换为使用特定字符集，请使用“'''ALTER TABLE'''”。为了成功进行转换，必…”的新页面</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=MySQL_%E5%AD%97%E7%AC%A6%E9%9B%86%EF%BC%9A%E5%88%97%E5%AD%97%E7%AC%A6%E9%9B%86%E8%BD%AC%E6%8D%A2&amp;diff=2172&amp;oldid=prev"/>
		<updated>2021-03-27T21:15:29Z</updated>

		<summary type="html">&lt;p&gt;建立内容为“&lt;a href=&quot;/%E5%88%86%E7%B1%BB:MySQL&quot; title=&quot;分类:MySQL&quot;&gt;category:MySQL&lt;/a&gt;  要将二进制或非二进制字符串列转换为使用特定字符集，请使用“&amp;#039;&amp;#039;&amp;#039;ALTER TABLE&amp;#039;&amp;#039;&amp;#039;”。为了成功进行转换，必…”的新页面&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[category:MySQL]]&lt;br /&gt;
&lt;br /&gt;
要将二进制或非二进制字符串列转换为使用特定字符集，请使用“'''ALTER TABLE'''”。为了成功进行转换，必须满足以下条件之一：&lt;br /&gt;
# 如果列具有二进制数据类型（BINARY，VARBINARY，BLOB），则必须使用单个字符集（将列转换为的字符集）对它包含的所有值进行编码。&lt;br /&gt;
#* 如果使用二进制列将信息存储在多个字符集中，则 MySQL 无法知道哪些值使用哪个字符集，并且无法正确转换数据。&lt;br /&gt;
# 如果列具有非二进制数据类型（CHAR，VARCHAR，TEXT），则其内容应以列字符集而不是其他字符集编码。&lt;br /&gt;
#* 如果内容以不同的字符集编码，则可以先将该列转换为使用二进制数据类型，然后再转换为具有所需字符集的非二进制列。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 如果该列包含两个字符集中都没有的字符，则转换可能是有损失的。&lt;br /&gt;
* 如果在最初创建列时指定了属性，则在用“ALTER TABLE”更改 table 时也应指定属性。例如，如果您指定“NOT NULL”和一个明确的“DEFAULT”值，则还应在“ALTER TABLE”语句中提供它们。否则，结果列定义将不包括那些属性。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
# 假设 tablet 的二进制列 col1 定义为 VARBINARY(50)。假设该列中的信息是使用单个字符集编码的，则可以将其转换为具有该字符集的非二进制列。例如，如果 col1 包含表示 greek 字符集中的字符的二进制数据，则可以按以下方式对其进行转换：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
ALTER TABLE t MODIFY col1 VARCHAR(50) CHARACTER SET greek;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 如果原始列的类型为 BINARY(50)，则可以将其转换为 CHAR(50)，但是结果值将在末尾添加 0x00 个字节，这可能是不希望的。要删除这些字节，请使用 TRIM() 函数：【？？？】&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
UPDATE t SET col1 = TRIM(TRAILING 0x00 FROM col1);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 假设 table t有一个名为 col1 的非二进制列，定义为 CHAR(50) CHARACTER SET latin1，但您希望将其转换为使用 utf8，以便可以存储多种语言的值。下面的语句完成了这一点：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
ALTER TABLE t MODIFY col1 CHAR(50) CHARACTER SET utf8;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 如果您有 MySQL 4.1 之前的旧 table【则这是一种特殊情况】。在该 table 中，非二进制列包含的值实际上是用与服务器默认字符集不同的字符集编码的。例如，即使 MySQL 的默认字符集不同，应用程序也可能在一列中存储了 sjis 值。可以将列转换为使用正确的字符集，但需要执行附加步骤。假设服务器的默认字符集是 latin1，并且 col1 定义为 CHAR(50)，但是其内容是 sjis 值：&lt;br /&gt;
## 第一步是将列转换为二进制数据类型，这将删除现有字符集信息，而无需执行任何字符转换：&lt;br /&gt;
##: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
ALTER TABLE t MODIFY col1 BLOB;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 下一步是使用适当的字符集将列转换为非二进制数据类型：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
ALTER TABLE t MODIFY col1 CHAR(50) CHARACTER SET sjis;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
#* 此过程要求在升级到 MySQL 4.1 或更高版本后，尚未使用“INSERT”或“UPDATE”等语句修改 table：在这种情况下，MySQL 将使用 latin1 将新值存储在该列中，并且该列将包含 sjis 和 latin1 值的混合，并且无法正确转换。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 要转换 table 中的所有字符列，“'''ALTER TABLE ... CONVERT TO CHARACTER SET charset'''”语句可能会有用。&lt;/div&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
</feed>