Rechercher dans le manuel MySQL
10.13.4.1 Defining a UCA Collation Using LDML Syntax
To add a UCA collation for a Unicode character set without recompiling MySQL, use the following procedure. If you are unfamiliar with the LDML rules used to describe the collation's sort characteristics, see Section 10.13.4.2, “LDML Syntax Supported in MySQL”.
The example adds a collation named
utf8_phone_ci
to the
utf8
character set. The collation is
designed for a scenario involving a Web application for which
users post their names and phone numbers. Phone numbers can be
given in very different formats:
+7-12345-67
+7-12-345-67
+7 12 345 67
+7 (12) 345 67
+71234567
The problem raised by dealing with these kinds of values is that the varying permissible formats make searching for a specific phone number very difficult. The solution is to define a new collation that reorders punctuation characters, making them ignorable.
Choose a collation ID, as shown in Section 10.13.2, “Choosing a Collation ID”. The following steps use an ID of 1029.
To modify the
Index.xml
configuration file. This file is located in the directory named by thecharacter_sets_dir
system variable. You can check the variable value as follows, although the path name might be different on your system:- +--------------------+-----------------------------------------+
- +--------------------+-----------------------------------------+
- +--------------------+-----------------------------------------+
Choose a name for the collation and list it in the
Index.xml
file. In addition, you'll need to provide the collation ordering rules. 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. Within the<collation>
element, provide a<rules>
element containing the ordering rules:<charset name="utf8"> ... <collation name="utf8_phone_ci" id="1029"> <rules> <reset>\u0000</reset> <i>\u0020</i> <!-- space --> <i>\u0028</i> <!-- left parenthesis --> <i>\u0029</i> <!-- right parenthesis --> <i>\u002B</i> <!-- plus --> <i>\u002D</i> <!-- hyphen --> </rules> </collation> ... </charset>
If you want a similar collation for other Unicode character sets, add other
<collation>
elements. For example, to defineucs2_phone_ci
, add a<collation>
element to the<charset name="ucs2">
element. Remember that each collation must have its own unique ID.Restart the server and use this statement to verify that the collation is present:
- +---------------+---------+------+---------+----------+---------+
- +---------------+---------+------+---------+----------+---------+
- | utf8_phone_ci | utf8 | 1029 | | | 8 |
- +---------------+---------+------+---------+----------+---------+
Now test the collation to make sure that it has the desired properties.
Create a table containing some sample phone numbers using the new collation:
- );
- Query OK, 0 rows affected (0.09 sec)
- Query OK, 1 row affected (0.00 sec)
- Query OK, 1 row affected (0.00 sec)
- Query OK, 1 row affected (0.00 sec)
- Query OK, 1 row affected (0.00 sec)
- Query OK, 1 row affected (0.00 sec)
Run some queries to see whether the ignored punctuation characters are in fact ignored for comparison and sorting:
- +-------+--------------------+
- | name | phone |
- +-------+--------------------+
- | Sanja | +380 (912) 8008005 |
- | Bar | +7-912-800-80-01 |
- | Svoj | +7 912 800 80 02 |
- | Ramil | (7912) 800 80 03 |
- | Hf | +7 (912) 800 80 04 |
- +-------+--------------------+
- +------+------------------+
- | name | phone |
- +------+------------------+
- | Bar | +7-912-800-80-01 |
- +------+------------------+
- +------+------------------+
- | name | phone |
- +------+------------------+
- | Bar | +7-912-800-80-01 |
- +------+------------------+
- +------+------------------+
- | name | phone |
- +------+------------------+
- | Bar | +7-912-800-80-01 |
- +------+------------------+
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-ldml-collation-example.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.