Several character set and collation system variables relate to a client's interaction with the server. Some of these have been mentioned in earlier sections:
- The server character set and collation can be determined from the values of the - character_set_serverand- collation_serversystem variables.
- The character set and collation of the default database can be determined from the values of the - character_set_databaseand- collation_databasesystem variables.
Additional character set and collation system variables are involved in handling traffic for the connection between a client and the server. Every client has connection-related character set and collation system variables.
Consider what a “connection” is: It is what you make when you connect to the server. The client sends SQL statements, such as queries, over the connection to the server. The server sends responses, such as result sets or error messages, over the connection back to the client. This leads to several questions about character set and collation handling for client connections, each of which can be answered in terms of system variables:
- What character set is the statement in when it leaves the client? - The server takes the - character_set_clientsystem variable to be the character set in which statements are sent by the client.
- What character set should the server translate a statement to after receiving it? - For this, the server uses the - character_set_connectionand- collation_connectionsystem variables. It converts statements sent by the client from- character_set_clientto- character_set_connection(except for string literals that have an introducer such as- _latin1or- _utf8).- collation_connectionis important for comparisons of literal strings. For comparisons of strings with column values,- collation_connectiondoes not matter because columns have their own collation, which has a higher collation precedence.
- What character set should the server translate to before shipping result sets or error messages back to the client? - The - character_set_resultssystem variable indicates the character set in which the server returns query results to the client. This includes result data such as column values, and result metadata such as column names and error messages.
Clients can fine-tune the settings for these variables, or depend on the defaults (in which case, you can skip the rest of this section). If you do not use the defaults, you must change the character settings for each connection to the server.
There are two statements that affect the connection-related character set variables as a group:
- SET NAMES '- charset_name' [COLLATE '- collation_name']- SET NAMESindicates what character set the client will use to send SQL statements to the server. Thus,- SET NAMES 'cp1251'tells the server, “future incoming messages from this client are in character set- cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a- SELECTstatement.)- A - SET NAMES 'statement is equivalent to these three statements:- x'- SET character_set_client = - x; SET character_set_results =- x; SET character_set_connection =- x;- Setting each of these character set variables also sets its corresponding collation variable to the default correlation for the character set. For example, setting - character_set_connectionto- xalso sets- collation_connectionto the default collation for- x. It is not necessary to set that collation explicitly. To specify a particular collation for the character sets, use the optional- COLLATEclause:- SET NAMES ' - charset_name' COLLATE '- collation_name'
- SET CHARACTER SET- charset_name- SET CHARACTER SETis similar to- SET NAMESbut sets- character_set_connectionand- collation_connectionto- character_set_databaseand- collation_database. A- SET CHARACTER SETstatement is equivalent to these three statements:- x- SET character_set_client = - x; SET character_set_results =- x; SET collation_connection = @@collation_database;- Setting - collation_connectionalso sets- character_set_connectionto the character set associated with the collation (equivalent to executing- SET character_set_connection = @@character_set_database). It is not necessary to set- character_set_connectionexplicitly.
          ucs2, utf16, and
          utf32 cannot be used as a client character
          set, which means that they do not work for SET
          NAMES or SET CHARACTER SET.
        
        The MySQL client programs mysql,
        mysqladmin, mysqlcheck,
        mysqlimport, and mysqlshow
        determine the default character set to use as follows:
      
- In the absence of other information, the programs use the compiled-in default character set, usually - latin1.
- The programs can autodetect which character set to use based on the operating system setting, such as the value of the - LANGor- LC_ALLlocale environment variable on Unix systems or the code page setting on Windows systems. For systems on which the locale is available from the OS, the client uses it to set the default character set rather than using the compiled-in default. For example, setting- LANGto- ru_RU.KOI8-Rcauses the- koi8rcharacter set to be used. Thus, users can configure the locale in their environment for use by MySQL clients.- The OS character set is mapped to the closest MySQL character set if there is no exact match. If the client does not support the matching character set, it uses the compiled-in default. For example, - ucs2is not supported as a connection character set.- C applications that wish to use character set autodetection based on the OS setting can invoke the following - mysql_options()call before connecting to the server:- mysql_options(mysql, MYSQL_SET_CHARSET_NAME, MYSQL_AUTODETECT_CHARSET_NAME);
- The programs support a - --default-character-setoption, which enables users to specify the character set explicitly to override whatever default the client otherwise determines.
        When a client connects to the server, it sends the name of the
        character set that it wants to use. The server uses the name to
        set the character_set_client,
        character_set_results, and
        character_set_connection system
        variables. In effect, the server performs a SET
        NAMES operation using the character set name.
      
        With the mysql client, if you want to use a
        character set different from the default, you could explicitly
        execute SET NAMES every time you start up.
        However, to accomplish the same result more easily, you can add
        the --default-character-set option
        setting to your mysql command line or in your
        option file. For example, the following option file setting
        changes the three connection-related character set variables set
        to koi8r each time you invoke
        mysql:
      
[mysql] default-character-set=koi8r
        If you are using the mysql client with
        auto-reconnect enabled (which is not recommended), it is
        preferable to use the charset command rather
        than SET NAMES. For example:
      
mysql> charset utf8
Charset changed
        The charset command issues a SET
        NAMES statement, and also changes the default
        character set that mysql uses when it
        reconnects after the connection has dropped.
      
        Example: Suppose that column1 is defined as
        CHAR(5) CHARACTER SET latin2. If you do not
        say SET NAMES or SET CHARACTER
        SET, then for SELECT column1 FROM
        t, the server sends back all the values for
        column1 using the character set that the
        client specified when it connected. On the other hand, if you
        say SET NAMES 'latin1' or SET
        CHARACTER SET latin1 before issuing the
        SELECT statement, the server
        converts the latin2 values to
        latin1 just before sending results back.
        Conversion may be lossy if there are characters that are not in
        both character sets.
      
        If you do not want the server to perform any conversion of
        result sets or error messages, set
        character_set_results to
        NULL or binary:
      
SET character_set_results = NULL;
To see the values of the character set and collation system variables that apply to your connection, use these statements:
SHOW VARIABLES LIKE 'character_set%'; SHOW VARIABLES LIKE 'collation%';
You must also consider the environment within which your MySQL applications execute. See Section 9.1.5, “Configuring the Character Set and Collation for Applications”.
For more information about character sets and error messages, see Section 9.1.6, “Character Set for Error Messages”.