Chapter 11. Character Set Support

Table of Contents

11.1. Character Sets and Collations in General
11.2. Character Sets and Collations in MySQL
11.3. Determining the Default Character Set and Collation
11.3.11.3.1. Server Character Set and Collation
11.3.11.3.2. Database Character Set and Collation
11.3.11.3.3. Table Character Set and Collation
11.3.11.3.4. Column Character Set and Collation
11.3.11.3.5. Examples of Character Set and Collation Assignment
11.3.11.3.6. Connection Character Sets and Collations
11.3.11.3.7. Character String Literal Character Set and Collation
11.3.11.3.8. Using COLLATE in SQL Statements
11.3.11.3.9. COLLATE Clause Precedence
11.3.11.3.10. BINARY Operator
11.3.11.3.11. Some Special Cases Where the Collation Determination Is Tricky
11.3.11.3.12. Collations Must Be for the Right Character Set
11.3.11.3.13. An Example of the Effect of Collation
11.4. Operations Affected by Character Set Support
11.4.11.4.1. Result Strings
11.4.11.4.2. CONVERT()
11.4.11.4.3. CAST()
11.4.11.4.4. SHOW Statements
11.5. Unicode Support
11.6. UTF8 for Metadata
11.7. Compatibility with Other DBMSs
11.8. New Character Set Configuration File Format
11.9. National Character Set
11.10. Upgrading Character Sets from MySQL 4.0
11.10.11.10.1. 4.0 Character Sets and Corresponding 4.1 Character Set/Collation Pairs
11.10.11.10.2. Converting 4.0 Character Columns to 4.1 Format
11.11. Character Sets and Collations That MySQL Supports
11.11.11.11.1. Unicode Character Sets
11.11.11.11.2. West European Character Sets
11.11.11.11.3. Central European Character Sets
11.11.11.11.4. South European and Middle East Character Sets
11.11.11.11.5. Baltic Character Sets
11.11.11.11.6. Cyrillic Character Sets
11.11.11.11.7. Asian Character Sets

Improved support for character set handling was added to MySQL in Version 4.1. The features described here are as implemented in MySQL 4.1.1. (MySQL 4.1.0 has some but not all of these features, and some of them are implemented differently.)

This chapter discusses the following topics:

Character set support currently is included in the MyISAM, MEMORY (HEAP), and (as of MySQL 4.1.2) InnoDB storage engines. The ISAM storage engine does not include character set support; there are no plans to change this, because ISAM is deprecated.

Character Sets and Collations in General

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, ‘c’ = 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.

Now, 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’. Now, 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's 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 case insensitivity but also accent insensitivity (an “accent” is a mark attached to a character as in German ‘Ö’) and multiple-character mappings (such as the rule that ‘Ö’ = ‘OE’ in one of the two German collations).

MySQL 4.1 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

  • Allow specification of character set and collation at any level

In these respects, not only is MySQL 4.1 far more flexible than MySQL 4.0, it also is far ahead of other DBMSs. However, to use the new features effectively, you will need to learn what character sets and collations are available, how to change their defaults, and what the various string operators do with them.