如何在不丢失或更改数据的情况下更改列排序规则?

How to change Column Collation without losing or changing data?

我一直在使用 mysql 版本 5.5.41,但 运行 遇到了问题。 我将 table 中特定列的排序规则从 latin1_swedish_ci 更改为 hebrew_bin,这会更改该列中的数据。例如,我在字段中插入了 école,在转换时,我得到了 ?cole。 所以我搜索了解决方案并找到了this。您可以看到它指出,为了在更改字符集和排序规则时不丢失数据,您必须先转换为 blob,然后再转换为所需的字符集。我也试过了,只是得到 יcole。那么如何在不丢失数据的情况下更改列排序规则。

这些是我对 blob 尝试的查询:-

ALTER TABLE `something` CHANGE `name` `name` BLOB;
ALTER TABLE `something` CHANGE `name` `name` VARCHAR(12) CHARACTER SET hebrew COLLATE hebrew_bin NOT NULL;

您必须将 CHANGE 更改为 MODIFY

第一步是将列转换为二进制数据类型,这会在不执行任何字符转换的情况下删除现有的字符集信息:

ALTER TABLE something MODIFY name BLOB;

下一步是将该列转换为具有适当字符集的非二进制数据类型:

ALTER TABLE something MODIFY name VARCHAR(12) CHARACTER SET hebrew COLLATE hebrew_bin;

或者试试这个:

ALTER TABLE something MODIFY name VARCHAR(12) CHARACTER SET utf8 COLLATE utf8_unicode_ci

阅读更多信息:

http://dev.mysql.com/doc/refman/5.5/en/charset-conversion.html

http://dev.mysql.com/doc/refman/5.5/en/charset-column.html

请注意,运行 对列的任何 MODIFYCHANGE 操作将(在实际意义上)删除 任何默认值或对该栏发表评论,as per the documentation.

When you use CHANGE or MODIFY, column_definition must include the data type and all attributes that should apply to the new column, other than index attributes such as PRIMARY KEY or UNIQUE. Attributes present in the original definition but not specified for the new definition are not carried forward.

SELECT HEX(col), col FROM ...查看为“école”存储的内容。 Latin1 看起来像 E9636F6C65。希伯来语,如果我没记错的话,不包括“é”。请参阅 http://collation-charts.org/mysql60/mysql604.hebrew_general_ci.html 了解可能支持的完整字符集。

假设这是正确的,请不要尝试转换为字符集希伯来语;你会丢失信息,比如 'é' 变成 '?'。

如果您需要同时存储希伯来语字符和法语重音字符(等),请使用 utf8。