SET NAMES

2023-10-24 09:23:03  Updated

Purpose

You can use this statement to set the character set and the collation for the current session.

Syntax

SET NAMES 'charset_name' [COLLATE 'collation_name'];

Parameters

Parameter Description
charset_name The character set.
collation_name The collation of the character set. If no collation is specified, the default collation of the character set is used.

Note

You can execute SHOW CHARSET; to query the character sets supported by the system, and execute SHOW COLLATION; to query the collations supported by the system.

Examples

  • Set the character set of the current session to gbk, and retain the default collation.

    obclient> SET NAMES 'gbk';
    Query OK, 0 rows affected
    
    obclient> SHOW VARIABLES LIKE 'character_set_c%';
    +--------------------------+-------+
     VARIABLE_NAME             VALUE 
    +--------------------------+-------+
     character_set_client      gbk   
     character_set_connection  gbk   
    +--------------------------+-------+
    2 rows in set
    
    obclient> SHOW VARIABLES LIKE 'collation_connection';
    +----------------------+----------------+
     VARIABLE_NAME         VALUE          
    +----------------------+----------------+
     collation_connection  gbk_chinese_ci 
    +----------------------+----------------+
    1 row in set
    
  • Set the character set to utf8mb4 and the collation to utf8mb4_general_ci for the current session.

    obclient> SET NAMES 'utf8mb4' COLLATE 'utf8mb4_general_ci';
    Query OK, 0 rows affected
    
    obclient> SHOW VARIABLES LIKE 'character_set_c%';
    +--------------------------+---------+
     VARIABLE_NAME             VALUE   
    +--------------------------+---------+
     character_set_client      utf8mb4 
     character_set_connection  utf8mb4 
    +--------------------------+---------+
    2 rows in set
    
    obclient> SHOW VARIABLES LIKE 'collation_connection';
    +----------------------+--------------------+
     VARIABLE_NAME         VALUE              
    +----------------------+--------------------+
     collation_connection  utf8mb4_general_ci 
    +----------------------+--------------------+
    1 row in set
    

Contact Us