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)

Find a PHP function

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

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

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.

Contents Haut