MySQL - SET NAMES Statement



A character set of MySQL is the set of legal characters that are allowed in a string. MySQL provides several character-sets you can see the list of all the available character sets using the SHOW CHARACTER SET statement.

SHOW CHARACTER SET\G;

Output

Following is the output of the above query −

************* 1. row *************
          Charset: armscii8
      Description: ARMSCII-8 Armenian
Default collation: armscii8_general_ci
           Maxlen: 1
************* 2. row *************
          Charset: ascii
      Description: US ASCII
Default collation: ascii_general_ci
           Maxlen: 1
************* 3. row *************
          Charset: big5
      Description: Big5 Traditional Chinese
Default collation: big5_chinese_ci
           Maxlen: 2
************* 4. row *************
          Charset: binary
      Description: Binary pseudo charset
Default collation: binary
           Maxlen: 1
************* 5. row *************
          Charset: cp1250
      Description: Windows Central European
Default collation: cp1250_general_ci
           Maxlen: 1
************* 6. row *************
          Charset: cp1251
      Description: Windows Cyrillic
Default collation: cp1251_general_ci
           Maxlen: 1
************* 7. row *************
          Charset: cp1256
      Description: Windows Arabic
Default collation: cp1256_general_ci
           Maxlen: 1
************* 8. row *************
          Charset: cp1257
      Description: Windows Baltic
Default collation: cp1257_general_ci
           Maxlen: 1
************* 9. row *************
          Charset: cp850
      Description: DOS West European
Default collation: cp850_general_ci
           Maxlen: 1
************* 10. row *************
          Charset: cp852
      Description: DOS Central European
Default collation: cp852_general_ci
           Maxlen: 1
************* 11. row *************
          Charset: cp866
      Description: DOS Russian
Default collation: cp866_general_ci
           Maxlen: 1
************* 12. row *************
          Charset: cp932
      Description: SJIS for Windows Japanese
Default collation: cp932_japanese_ci
           Maxlen: 2
************* 13. row *************
          Charset: dec8
      Description: DEC West European
Default collation: dec8_swedish_ci
           Maxlen: 1
************* 14. row *************
          Charset: eucjpms
      Description: UJIS for Windows Japanese
Default collation: eucjpms_japanese_ci
           Maxlen: 3
************* 15. row *************
          Charset: euckr
      Description: EUC-KR Korean
Default collation: euckr_korean_ci
           Maxlen: 2
************* 16. row *************
          Charset: gb18030
      Description: China National Standard GB18030
Default collation: gb18030_chinese_ci
           Maxlen: 4
************* 17. row *************
          Charset: gb2312
      Description: GB2312 Simplified Chinese
Default collation: gb2312_chinese_ci
           Maxlen: 2
************* 18. row *************
          Charset: gbk
      Description: GBK Simplified Chinese
Default collation: gbk_chinese_ci
           Maxlen: 2
************* 19. row *************
          Charset: geostd8
      Description: GEOSTD8 Georgian
Default collation: geostd8_general_ci
           Maxlen: 1
************* 20. row *************
          Charset: greek
      Description: ISO 8859-7 Greek
Default collation: greek_general_ci
           Maxlen: 1
************* 21. row *************
          Charset: hebrew
      Description: ISO 8859-8 Hebrew
Default collation: hebrew_general_ci
           Maxlen: 1
************* 22. row *************
          Charset: hp8
      Description: HP West European
Default collation: hp8_english_ci
           Maxlen: 1
************* 23. row *************
          Charset: keybcs2
      Description: DOS Kamenicky Czech-Slovak
Default collation: keybcs2_general_ci
           Maxlen: 1
************* 24. row *************
          Charset: koi8r
      Description: KOI8-R Relcom Russian
Default collation: koi8r_general_ci
           Maxlen: 1
************* 25. row *************
          Charset: koi8u
      Description: KOI8-U Ukrainian
Default collation: koi8u_general_ci
           Maxlen: 1
************* 26. row *************
          Charset: latin1
      Description: cp1252 West European
Default collation: latin1_swedish_ci
           Maxlen: 1
************* 27. row *************
          Charset: latin2
      Description: ISO 8859-2 Central European
Default collation: latin2_general_ci
           Maxlen: 1
************* 28. row *************
          Charset: latin5
      Description: ISO 8859-9 Turkish
Default collation: latin5_turkish_ci
           Maxlen: 1
************* 29. row *************
          Charset: latin7
      Description: ISO 8859-13 Baltic
Default collation: latin7_general_ci
           Maxlen: 1
************* 30. row *************
          Charset: macce
      Description: Mac Central European
Default collation: macce_general_ci
           Maxlen: 1
************* 31. row *************
          Charset: macroman
      Description: Mac West European
Default collation: macroman_general_ci
           Maxlen: 1
************* 32. row *************
          Charset: sjis
      Description: Shift-JIS Japanese
Default collation: sjis_japanese_ci
           Maxlen: 2
************* 33. row *************
          Charset: swe7
      Description: 7bit Swedish
Default collation: swe7_swedish_ci
           Maxlen: 1
************* 34. row *************
          Charset: tis620
      Description: TIS620 Thai
Default collation: tis620_thai_ci
           Maxlen: 1
************* 35. row *************
          Charset: ucs2
      Description: UCS-2 Unicode
Default collation: ucs2_general_ci
           Maxlen: 2
************* 36. row *************
          Charset: ujis
      Description: EUC-JP Japanese
Default collation: ujis_japanese_ci
           Maxlen: 3
************* 37. row *************
          Charset: utf16
      Description: UTF-16 Unicode
Default collation: utf16_general_ci
           Maxlen: 4
************* 38. row *************
          Charset: utf16le
      Description: UTF-16LE Unicode
Default collation: utf16le_general_ci
           Maxlen: 4
************* 39. row *************
          Charset: utf32
      Description: UTF-32 Unicode
Default collation: utf32_general_ci
           Maxlen: 4
************* 40. row *************
          Charset: utf8mb3
      Description: UTF-8 Unicode
Default collation: utf8mb3_general_ci
           Maxlen: 3
************* 41. row *************
          Charset: utf8mb4
      Description: UTF-8 Unicode
Default collation: utf8mb4_0900_ai_ci
           Maxlen: 4

You can get the name of the default character set using the following query −

SELECT default_character_set_name FROM information_schema.SCHEMATA S 
WHERE schema_name = "test";

Output

The above query produces the following output −

DEFAULT_CHARACTER_SET_NAME
utf8mb4

The SET NAMES Statement

The MySQL SET NAMES Statement is used to set values to the character_set_client, character_set_connection, and character_set_results session system variables.

Syntax

Following is the syntax of the MySQL SET NAMES Statement −

SET NAMES {'charset_name'
[COLLATE 'collation_name'] | DEFAULT}

Where, 'charset_name' is the name of the character set.

Example

Following query sets the values to the character_set_client, character_set_connection, and character_set_results variables to 'utf8'. −

SET NAMES 'utf8';

Verification

You can verify the current value of the character_set_client and character_set_results variables using the show variables like statement as shown below −

show variables like "character_set_client";

Output

After executing the above mysql query, it produces the output shown below −

Variable_name Value
character_set_client utf8
show variables like "character_set_results";

Output

Following is the output of the above query −

Variable_name Value
character_set_results utf8
show variables like "character_set_connection";

Output

The above mysql query produces the following output −

Variable_name Value
character_set_connection utf8

The collate clause

MySQL collation is a set of rules to verify/compare each character of a character set. Each character set have at least one collation (it can have more) and a default collation. No two-character sets can have same collation. SHOW CHARACTER SET statement. provides the default collation for every character set.

You can specify a collation to a character set explicitly, using the COLLATE clause.

Example

Following query sets the collation of the character set to ascii_bin −

SET NAMES ascii COLLATE ascii_bin;

You can get the all the allowed collations for a character set using the SHOW COLLATION statement as follows −

SHOW COLLATION WHERE Charset = 'ascii';

Output

After executing the above query, it will produce the following output −

Collation Charset Id Default Compiled Sortlen Pad_attribute
ascii_bin ascii 65 Yes 1 PAD SPACE
ascii_general_ci ascii 11 Yes Yes 1 PAD SPACE

The DEFAULT clause

You can set the collation and mapping to the default values using the DEFAULT clause.

SET NAMES DEFAULT;
Advertisements