MySQL - SET CHARACTER SET 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

The above mysql query produces the following output −

*********** 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

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

DEFAULT_CHARACTER_SET_NAME
utf8mb4

The SET CHARACTER SET Statement

The SET CHARACTER SET Statement of MySQL is used to assign a value to the character set attribute. It maps all the strings between the server and the current client with the specified mapping set. This statement changes values of the character_set_client and character_set_results variables.

Syntax

Following is the syntax of the MySQL SET CHARACTER SET Statement −

SET {CHARACTER SET | CHARSET} {'charset_name' | DEFAULT}

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

Example

Following query sets the character set to macroman −

SET CHARACTER SET macroman;

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

The above query produces the following output −

Variable_name Value
character_set_client macroman
show variables like "character_set_results";

Output

Following is the output of the above mysql query −

Variable_name Value
character_set_results macroman

The DEFAULT value

You can set the character set mapping to the default one of the databases using the DEFAULT as value as shown below −

SET CHARACTER SET DEFAULT;

Since the default character set is utf8mb4, the values of the character_set_client and character_set_results variables are set to it. You can verify these values as shown below −

show variables like "character_set_results";

Output

After executing the above query, it produces the following output −

Variable_name Value
character_set_results utf8mb4
show variables like "character_set_client";

Output

The above mysql query will generate the output shown below −

Variable_name Value
character_set_client utf8mb4

SET CHARSET statement

You can also use the CHARSET keyword instead of CHARACTER SET as shown below −

SET CHARSET latin7;

Verification

Let us verify the current value of the character_set_client or, character_set_results variables using the show variables like statement as shown below −

show variables like "character_set_client";

Output

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

Variable_name Value
character_set_client latin7
Advertisements