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.

  1. Choose a collation ID, as shown in Section 10.13.2, “Choosing a Collation ID”. The following steps use an ID of 1029.

  2. To modify the Index.xml configuration file. This file is located in the directory named by the character_sets_dir system variable. You can check the variable value as follows, although the path name might be different on your system:

    1. mysql> SHOW VARIABLES LIKE 'character_sets_dir';
    2. +--------------------+-----------------------------------------+
    3. | Variable_name      | Value                                   |
    4. +--------------------+-----------------------------------------+
    5. | character_sets_dir | /user/local/mysql/share/mysql/charsets/ |
    6. +--------------------+-----------------------------------------+
  3. 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>
  4. If you want a similar collation for other Unicode character sets, add other <collation> elements. For example, to define ucs2_phone_ci, add a <collation> element to the <charset name="ucs2"> element. Remember that each collation must have its own unique ID.

  5. Restart the server and use this statement to verify that the collation is present:

    1. mysql> SHOW COLLATION WHERE Collation = 'utf8_phone_ci';
    2. +---------------+---------+------+---------+----------+---------+
    3. | Collation     | Charset | Id   | Default | Compiled | Sortlen |
    4. +---------------+---------+------+---------+----------+---------+
    5. | utf8_phone_ci | utf8    | 1029 |         |          |       8 |
    6. +---------------+---------+------+---------+----------+---------+

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:

  1. mysql> CREATE TABLE phonebook (
  2.          name VARCHAR(64),
  3.          phone VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_phone_ci
  4.        );
  5. Query OK, 0 rows affected (0.09 sec)
  6.  
  7. mysql> INSERT INTO phonebook VALUES ('Svoj','+7 912 800 80 02');
  8. Query OK, 1 row affected (0.00 sec)
  9.  
  10. mysql> INSERT INTO phonebook VALUES ('Hf','+7 (912) 800 80 04');
  11. Query OK, 1 row affected (0.00 sec)
  12.  
  13. mysql> INSERT INTO phonebook VALUES ('Bar','+7-912-800-80-01');
  14. Query OK, 1 row affected (0.00 sec)
  15.  
  16. mysql> INSERT INTO phonebook VALUES ('Ramil','(7912) 800 80 03');
  17. Query OK, 1 row affected (0.00 sec)
  18.  
  19. mysql> INSERT INTO phonebook VALUES ('Sanja','+380 (912) 8008005');
  20. 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:

  1. mysql> SELECT * FROM phonebook ORDER BY phone;
  2. +-------+--------------------+
  3. | name  | phone              |
  4. +-------+--------------------+
  5. | Sanja | +380 (912) 8008005 |
  6. | Bar   | +7-912-800-80-01   |
  7. | Svoj  | +7 912 800 80 02   |
  8. | Ramil | (7912) 800 80 03   |
  9. | Hf    | +7 (912) 800 80 04 |
  10. +-------+--------------------+
  11. 5 rows in set (0.00 sec)
  12.  
  13. mysql> SELECT * FROM phonebook WHERE phone='+7(912)800-80-01';
  14. +------+------------------+
  15. | name | phone            |
  16. +------+------------------+
  17. | Bar  | +7-912-800-80-01 |
  18. +------+------------------+
  19. 1 row in set (0.00 sec)
  20.  
  21. mysql> SELECT * FROM phonebook WHERE phone='79128008001';
  22. +------+------------------+
  23. | name | phone            |
  24. +------+------------------+
  25. | Bar  | +7-912-800-80-01 |
  26. +------+------------------+
  27. 1 row in set (0.00 sec)
  28.  
  29. mysql> SELECT * FROM phonebook WHERE phone='7 9 1 2 8 0 0 8 0 0 1';
  30. +------+------------------+
  31. | name | phone            |
  32. +------+------------------+
  33. | Bar  | +7-912-800-80-01 |
  34. +------+------------------+
  35. 1 row in set (0.00 sec)

Rechercher dans le manuel MySQL

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

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

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.

Table des matières Haut