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 |
- +------+------------------+
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-ldml-collation-example.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.