本节介绍如何通过在 MySQL Index.xml 文件中为 <charset> 字符集描述编写 <collation> 元素来为 8 位字符集添加简单排序规则。此处描述的过程无需重新编译 MySQL。示例为 latin1 字符集添加名为 latin1_test_ci 的排序规则。This section describes how to add a simple collation for an
8-bit character set by writing the
<collation> elements associated with a
<charset> character set description in
the MySQL Index.xml file. The procedure
described here does not require recompiling MySQL. The example
adds a collation named latin1_test_ci to the
latin1 character set.
选择一个排序规则 ID,如 第 12.14.2 节,“选择排序规则 ID” 所示。以下步骤使用 ID 1024。Choose a collation ID, as shown in Section 12.14.2, “Choosing a Collation ID”. The following steps use an ID of 1024.
修改
Index.xml和latin1.xml配置文件。这些文件位于由character_sets_dir系统变量指定的目录中。您可以按如下方式检查该变量的值,不过路径名在您的系统上可能不同:Modify theIndex.xmlandlatin1.xmlconfiguration files. These files are located in the directory named by thecharacter_sets_dirsystem variable. You can check the variable value as follows, although the path name might be different on your system:mysql>
SHOW VARIABLES LIKE 'character_sets_dir';+--------------------+-----------------------------------------+ | Variable_name | Value | +--------------------+-----------------------------------------+ | character_sets_dir | /user/local/mysql/share/mysql/charsets/ | +--------------------+-----------------------------------------+为排序规则选择一个名称,并将其列在
Index.xml文件中。找到要添加排序规则的字符集的<charset>元素,然后添加一个<collation>元素,该元素指明排序规则名称和 ID,以将名称与 ID 关联。例如:Choose a name for the collation and list it in theIndex.xmlfile. Find the<charset>element for the character set to which the collation is being added, and add a<collation>element that indicates the collation name and ID, to associate the name with the ID. For example:<charset name="latin1"> ... <collation name="latin1_test_ci" id="1024"/> ... </charset>
在
latin1.xml配置文件中,添加一个<collation>元素,该元素命名排序规则,并包含一个<map>元素,该元素为字符代码 0 到 255 定义字符代码到权重的映射表。<map>元素中的每个值必须是十六进制格式的数字。In thelatin1.xmlconfiguration file, add a<collation>element that names the collation and that contains a<map>element that defines a character code-to-weight mapping table for character codes 0 to 255. Each value within the<map>element must be a number in hexadecimal format.<collation name="latin1_test_ci"> <map> 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF 41 41 41 41 5B 5D 5B 43 45 45 45 45 49 49 49 49 44 4E 4F 4F 4F 4F 5C D7 5C 55 55 55 59 59 DE DF 41 41 41 41 5B 5D 5B 43 45 45 45 45 49 49 49 49 44 4E 4F 4F 4F 4F 5C F7 5C 55 55 55 59 59 DE FF </map> </collation>
重启服务器,并使用此语句来验证排序规则是否存在:Restart the server and use this statement to verify that the collation is present:
mysql>
SHOW COLLATION WHERE Collation = 'latin1_test_ci';+----------------+---------+------+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +----------------+---------+------+---------+----------+---------+ | latin1_test_ci | latin1 | 1024 | | | 1 | +----------------+---------+------+---------+----------+---------+