Chapter 124. Determining the Default Character Set and Collation

Table of Contents

Server Character Set and Collation
Database Character Set and Collation
Table Character Set and Collation
Column Character Set and Collation
Examples of Character Set and Collation Assignment
Example 1: Table + Column Definition
Example 2: Table + Column Definition
Example 3: Table + Column Definition
Example 4: Database + Table + Column Definition
Connection Character Sets and Collations
Character String Literal Character Set and Collation
COLLATE Clause in Various Parts of an SQL Query
COLLATE Clause Precedence
BINARY Operator
Some Special Cases Where the Collation Determination is Tricky
Collations Must Be for the Right Character Set
An example of the Effect of Collation

There are default settings for character sets and collations at four levels: server, database, table, connection. The following description may appear complex, but it's been found in practice that multi-level defaulting leads to natural and obvious results.

Server Character Set and Collation

The MySQL Server has a server character set and a server collation, which may not be null.

MySQL determines the server character set and server collation thus:

  • According to the option settings in effect when the server starts up.

At this level, the decision is simple. The server character set and collation depend on the options that you use when you start mysqld. You can use --default-character-set=character_set_name for the character set, and along with it you can add --default-collation=collation_name for the collation. If you don't specify a character set, that is the same as saying --default-character-set=latin1. If you specify only a character set (for instance, latin1) but not a collation, that is the same as saying --default-charset=latin1 --collation=latin1_swedish_ci because latin1_swedish_ci is the default collation for latin1. Therefore the following three commands all have the same effect:

shell> mysqld
shell> mysqld --default-character-set=latin1
shell> mysqld --default-character-set=latin1
           --default-collation=latin1_swedish_ci

One way to change the settings is by recompiling. If you want to change the default server character set and collation when building from sources, use: --with-character-set and --with-collation as arguments for configure. For example:

shell> ./configure --with-character-set=latin1

or:

shell> ./configure --with-character-set=latin1
           --with-collation=latin1_german1_ci

Both mysqld and configure check that the character set/collation combination is valid. Each program displays an error message and terminates if the combination is not valid.