MySQL :: MySQL 8.0 Reference Manual :: 10.8.6 Examples of the Effect of Collation https://dev.mysql.com/doc/refman/8.0/en/charset-collation-effect.html
10.8.6 Examples of the Effect of Collation
Example 1: Sorting German Umlauts
Suppose that column X
in table T
has these latin1
column values:
Muffler
Müller
MX Systems
MySQL
Suppose also that the column values are retrieved using the following statement:
SELECT X FROM T ORDER BY X COLLATE collation_name;
The following table shows the resulting order of the values if we use ORDER BY
with different collations.
latin1_swedish_ci | latin1_german1_ci | latin1_german2_ci |
---|---|---|
Muffler | Muffler | Müller |
MX Systems | Müller | Muffler |
Müller | MX Systems | MX Systems |
MySQL | MySQL | MySQL |
The character that causes the different sort orders in this example is the U with two dots over it (ü
), which the Germans call “U-umlaut.”
-
The first column shows the result of the
SELECT
using the Swedish/Finnish collating rule, which says that U-umlaut sorts with Y. -
The second column shows the result of the
SELECT
using the German DIN-1 rule, which says that U-umlaut sorts with U. -
The third column shows the result of the
SELECT
using the German DIN-2 rule, which says that U-umlaut sorts with UE.
Example 2: Searching for German Umlauts
Suppose that you have three tables that differ only by the character set and collation used:
mysql> SET NAMES utf8;
mysql> CREATE TABLE german1 (
c CHAR(10)
) CHARACTER SET latin1 COLLATE latin1_german1_ci;
mysql> CREATE TABLE german2 (
c CHAR(10)
) CHARACTER SET latin1 COLLATE latin1_german2_ci;
mysql> CREATE TABLE germanutf8 (
c CHAR(10)
) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Each table contains two records:
mysql> INSERT INTO german1 VALUES ('Bar'), ('Bär');
mysql> INSERT INTO german2 VALUES ('Bar'), ('Bär');
mysql> INSERT INTO germanutf8 VALUES ('Bar'), ('Bär');
Two of the above collations have an A = Ä
equality, and one has no such equality (latin1_german2_ci
). For that reason, you'll get these results in comparisons:
mysql> SELECT * FROM german1 WHERE c = 'Bär';
+------+
| c |
+------+
| Bar |
| Bär |
+------+
mysql> SELECT * FROM german2 WHERE c = 'Bär';
+------+
| c |
+------+
| Bär |
+------+
mysql> SELECT * FROM germanutf8 WHERE c = 'Bär';
+------+
| c |
+------+
| Bar |
| Bär |
+------+
This is not a bug but rather a consequence of the sorting properties of latin1_german1_ci
and utf8_unicode_ci
(the sorting shown is done according to the German DIN 5007 standard).
MySQL :: MySQL 8.0 Reference Manual :: 10.1 Character Sets and Collations in General https://dev.mysql.com/doc/refman/8.0/en/charset-general.html
A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set. Let's make the distinction clear with an example of an imaginary character set.
Suppose that we have an alphabet with four letters: A
, B
, a
, b
. We give each letter a number: A
= 0, B
= 1, a
= 2, b
= 3. The letter A
is a symbol, the number 0 is the encoding for A
, and the combination of all four letters and their encodings is a character set.
Suppose that we want to compare two string values, A
and B
. The simplest way to do this is to look at the encodings: 0 for A
and 1 for B
. Because 0 is less than 1, we say A
is less than B
. What we've just done is apply a collation to our character set. The collation is a set of rules (only one rule in this case): “compare the encodings.” We call this simplest of all possible collations a binary collation.
But what if we want to say that the lowercase and uppercase letters are equivalent? Then we would have at least two rules: (1) treat the lowercase letters a
and b
as equivalent to A
and B
; (2) then compare the encodings. We call this a case-insensitive collation. It is a little more complex than a binary collation.
In real life, most character sets have many characters: not just A
and B
but whole alphabets, sometimes multiple alphabets or eastern writing systems with thousands of characters, along with many special symbols and punctuation marks. Also in real life, most collations have many rules, not just for whether to distinguish lettercase, but also for whether to distinguish accents (an “accent” is a mark attached to a character as in German Ö
), and for multiple-character mappings (such as the rule that Ö
= OE
in one of the two German collations).
MySQL can do these things for you:
-
Store strings using a variety of character sets.
-
Compare strings using a variety of collations.
-
Mix strings with different character sets or collations in the same server, the same database, or even the same table.
-
Enable specification of character set and collation at any level.
To use these features effectively, you must know what character sets and collations are available, how to change the defaults, and how they affect the behavior of string operators and functions.