content_image/823670.jpg
SET CHARACTER SET  is similar to SET NAMES  but sets character_set_connection  and collation_connection  to character_set_database  and collation_database  (which, as mentioned previously, indicate the character set and collation of the default database). 

A SET CHARACTER SET charset_name  statement is equivalent to these three statements: 

SET character_set_client = charset_name ; SET character_set_results = charset_name ; SET collation_connection = @@collation_database; 

Setting collation_connection  also implicitly sets character_set_connection  to the character set associated with the collation (equivalent to executing SET character_set_connection = @@character_set_database ). It is unnecessary to set character_set_connection  explicitly. 

<img src='content_image/823670.jpg'>

## Note 

Some character sets cannot be used as the client character set. Attempting to use them with SET NAMES  or SET CHARACTER SET  produces an error. See Impermissible Client Character Sets. 

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 for characters that are not in both character sets. 

## Connection Character Set Error Handling 

Attempts to use an inappropriate connection character set or collation can produce an error, or cause the server to fall back to its default character set and collation for a given connection. This section describes problems that can occur when configuring the connection character set. These problems can occur when establishing a connection or when changing the character set within an established connection. 

## Connect-Time Error Handling 

•Connect-Time Error Handling 

•Runtime Error Handling 

Some character sets cannot be used as the client character set; see Impermissible Client Character Sets. If you specify a character set that is valid but not permitted as a client character set, the server returns an error: 

shell> mysql --default-character-set=ucs2 ERROR 1231 (42000): Variable ’character_set_client’ can’t be set to the value of ’ucs2’ 

If you specify a character set that the client does not recognize, it produces an error: 

shell> mysql --default-character-set=bogus mysql: Character set ’bogus’ is not a compiled character set and is not specified in the ’/usr/local/mysql/share/charsets/Index.xml’ file ERROR 2019 (HY000): Can’t initialize character set bogus (path: /usr/local/mysql/share/charsets/) 

If you specify a character set that the client recognizes but the server does not, the server falls back to its default character set and collation. Suppose that the server is configured to use latin1  and latin1_swedish_ci  as its defaults, and that it does not recognize gb18030  as a valid character set. 

