DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(mysql.info.gz) Charset-connection

Info Catalog (mysql.info.gz) Charset-examples (mysql.info.gz) Charset-defaults (mysql.info.gz) Charset-literal
 
 10.3.6 Connection Character Sets and Collations
 -----------------------------------------------
 
 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 are available as the values
      of the `character_set_server' and `collation_server' variables.
 
    * The character set and collation of the default database are
      available as the values of the `character_set_database' and
      `collation_database' variables.
 
 
 Additional character set and collation variables are involved in
 handling traffic for the connection between a client and the server.
 Every client has connection-related character set and collation
 variables.
 
 Consider what a "connection" is: It's 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, 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 query in when it leaves the client?
 
      The server takes the `character_set_client' variable to be the
      character set in which queries are sent by the client.
 
    * What character set should the server translate a query to after
      receiving it?
 
      For this, `character_set_connection' and `collation_connection'
      are used by the server.  It converts queries sent by the client
      from `character_set_client' to `character_set_connection' (except
      for string literals that have an introducer such as `_latin1' or
      `_utf8').  `collation_connection' is important for comparisons of
      literal strings.  For comparisons of strings with column values,
      it does not matter because columns have 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_results' 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.
 
 
 You can fine-tune the settings for these variables, or you can depend
 on the defaults (in which case, you can skip this section).
 
 There are two statements that affect the connection character sets:
 
      SET NAMES 'CHARSET_NAME'
      SET CHARACTER SET CHARSET_NAME
 
 `SET NAMES' indicates what is in the SQL statements that the client
 sends. Thus, `SET NAMES 'cp1251'' tells the server "future incoming
 messages from this client will be in character set `cp1251'." It also
 specifies the character set for results that the server sends back to
 the client.  (For example, it indicates what character set column values
 will have if you use a `SELECT' statement.)
 
 A `SET NAMES 'X'' statement is equivalent to these three statements:
 
      mysql> SET character_set_client = X;
      mysql> SET character_set_results = X;
      mysql> SET character_set_connection = X;
 
 Setting `character_set_connection' to `x' also sets
 `collation_connection' to the default collation for `x'.
 
 `SET CHARACTER SET' is similar but sets the connection character set
 and collation to be those of the default database.  A `SET CHARACTER SET
 x' statement is equivalent to these three statements:
 
      mysql> SET character_set_client = X;
      mysql> SET character_set_results = X;
      mysql> SET collation_connection = @@collation_database;
 
 When a client connects, it sends to the server the name of the
 character set that it wants to use. The server sets the
 `character_set_client', `character_set_results', and
 `character_set_connection' variables to that character set. (In effect,
 the server performs a `SET NAMES' operation using the character set.)
 
 With the `mysql' client, it is not necessary to execute `SET NAMES'
 every time you start up if you want to use a character set different
 from the default. You can add the `--default-character-set'  option
 setting to your `mysql' statement line, or in your option file.  For
 example, the following option file setting changes the three character
 set variables set to `koi8r' each time you run `mysql':
 
      [mysql]
      default-character-set=koi8r
 
 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 will send 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', then just before sending results back, the
 server will convert the `latin2' values to `latin1'.  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, set
 `character_set_results' to `NULL':
 
      mysql> SET character_set_results = NULL;
 
Info Catalog (mysql.info.gz) Charset-examples (mysql.info.gz) Charset-defaults (mysql.info.gz) Charset-literal
automatically generated byinfo2html