MySQL Character Set

Source: Internet
Author: User
Tags mysql commands
MySQL character set support has two aspects:
Character Set and collation ).
The support for character sets is refined to four layers:
Server, database, table, and connection ).

1. Default MySQL Character Set

MySQL can refine the character set designation to a database, a table, and a column.

However, traditional programs do not use such complex configurations when creating databases and data tables. They use the default configuration. So where does the default configuration come from?

(1) When compiling MySQL, a default character set is specified, which is Latin1;
(2) When installing MySQL, you can specify a default character set in the configuration file (My. INI). If this character set is not specified, this value inherits from the one specified during compilation;
(3) When starting mysqld, you can specify a default character set in the command line parameters. If this parameter is not specified, this value inherits from the configuration in the configuration file.Character_set_serverSet to the default character set;
(4) When creating a new database, unless explicitly specified, the character set of this database is setCharacter_set_server;
(5) When a database is selected,Character_set_databaseSet to the default Character Set of this database;
(6) When creating a table in this database, the default Character Set of the table is setCharacter_set_databaseThat is, the default Character Set of the database;
(7) When setting a column in a table, unless explicitly specified, the default character set in this column is the default Character Set of the table;

Simply put, if you do not modify anything, all the columns of all tables in all databases will be stored in Latin1. However, if you install MySQL, you will generally choose multi-language support, that is to say, the installer automatically addsDefault_character_setSet to UTF-8, which ensures that all columns of all tables in all databases are stored in UTF-8 by default.

2. view the default character set. (by default, the MySQL character set is Latin1 (iso_8859_1)
Generally, you can run the following two commands to view the character set and sorting method of the system:
Mysql> show variables like 'character % ';
+ -------------------------- + ----------------------------------- +
| Variable_name | value |
+ -------------------------- + ----------------------------------- +
| Character_set_client | Latin1 |
| Character_set_connection | Latin1 |
| Character_set_database | Latin1 |
| Character_set_filesystem | binary |
| Character_set_results | Latin1 |
| Character_set_server | Latin1 |
| Character_set_system | utf8 |
| Character_sets_dir | D: "mysql-5.0.37" share "charsets" |
+ -------------------------- + ----------------------------------- +

Mysql> show variables like 'collation _ % ';
+ ---------------------- + ----------------- +
| Variable_name | value |
+ ---------------------- + ----------------- +
| Collation_connection | utf8_general_ci |
| Collation_database | utf8_general_ci |
| Collation_server | utf8_general_ci |
+ ---------------------- + ----------------- +

3. Modify the default Character Set
(1) The simplest modification method is to modify the character set key value in MySQL's my. ini file,
For example, default-character-set = utf8
Character_set_server = utf8
After modification, restart the MySQL service, service MySQL restart
Use mysql> show variables like 'character % '; Check that the database encoding has been changed to utf8
+ -------------------------- + ----------------------------------- +
| Variable_name | value |
+ -------------------------- + ----------------------------------- +
| Character_set_client | utf8 |
| Character_set_connection | utf8 |
| Character_set_database | utf8 |
| Character_set_filesystem | binary |
| Character_set_results | utf8 |
| Character_set_server | utf8 |
| Character_set_system | utf8 |
| Character_sets_dir | D: "mysql-5.0.37" share "charsets" |
+ -------------------------- + ----------------------------------- +

(2) another way to modify the character set is to use MySQL commands.
Mysql> set character_set_client = utf8;
Mysql> set character_set_connection = utf8;
Mysql> set character_set_database = utf8;
Mysql> set character_set_results = utf8;
Mysql> set character_set_server = utf8;

Mysql> set collation_connection = utf8;
Mysql> set collation_database = utf8;
Mysql> set collation_server = utf8;

Generally, even if the default Character Set of the table is set to utf8 and the query is sent through the UTF-8 encoding, you will find that the database is still garbled. The problem lies in the connection layer. The solution is to execute the following statement before sending the query:

Set names 'utf8 ';

It is equivalent to the following three commands:
Set character_set_client = utf8;
Set character_set_results = utf8;
Set character_set_connection = utf8;

Summary:
Therefore, what database version is used, whether it is 3.x, 4.0.x or 4.1.x, is not important to us. There are two important aspects:
(1) correctly set the database encoding. the character set for versions earlier than mysql4.0 is always the default ISO8859-1, mysql4.1 will let you choose when installing. If you are going to use UTF-8, you need to specify the UTF-8 when creating the database (you can also change it after the creation, 4.1 or later versions can also separately specify the character set of the table)
2) correctly set the database connection encoding. After setting the database encoding, you should specify the connection encoding when connecting to the database, for example, specifying the connection as utf8 during JDBC connection.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.