How to view and modify the encoding of the current database in MySQL
In MySQL, database encoding is a very important issue. Sometimes we need to check the encoding of the current database, or even modify the database encoding.
The SQL statement for viewing the current database code is:
Mysql> use xxx
Database changed
Mysql> show variables like 'character _ set_database ';
+ ------------------------ + -------- +
| Variable_name | Value |
+ ------------------------ + -------- +
| Character_set_database | latin1 |
+ ------------------------ + -------- +
1 row in set (0.00 sec)
Above, we first switch to the xxx database, and then use the SQL statement: show variables like 'character _ set_database '; to view the encoding of the xxx database. The query result is latin1 encoding.
Next, we will modify the encoding of the xxx database and change it to gb2312.
Mysql> alter database xxx character set gb2312;
Query OK, 1 row affected (0.00 sec)
Mysql> show variables like 'character _ set_database ';
+ ------------------------ + -------- +
| Variable_name | Value |
+ ------------------------ + -------- +
| Character_set_database | gb2312 |
+ ------------------------ + -------- +
1 row in set (0.00 sec)
Two things are also done here:
1. Use the SQL statement: alter database xxx CHARACTER SET gb2312; SET the encoding of the xxx database to gb2312.
2. Use show variables like 'character _ set_database 'again to check the current xxx encoding. After confirmation, the database code has been changed to gb2312.
3. Of course, in many cases, it is changed to UTF-8 encoding.
This article describes how to view and modify the encoding of the current database in MySQL. I hope it will help you. Thank you!