Solve Chinese insert problem in MySQL

Source: Internet
Author: User

1. Set up the character set when MySQL is installed

MySQL database is installed at the time. Select the language as UTF8 or GBK so that you can store Chinese in the database.

2. Setting the character set when creating a table

For example:

CREATE TABLE T_department (

Sid varchar Not NULL,

PID varchar not NULL,

Thedata varchar (m) Not NULL

) Engine=innodb DEFAULT Charset=utf8;

3. Change the field character set property in a table

Take the Users table for example: 1. View Table Properties

Mysql> Show create table users;
+-------+-----------------------------------------------------------------------
-------------------------- ----------------------------------------------------+
| Table |  Create Table
|
+-------+-----------------------------------------------------------------------
-------------------------- ----------------------------------------------------+
| users | CREATE TABLE ' users ' (
' userid ' int () default NULL,
' username ' char () character Set latin1 default Null
  ) Engine=innodb DEFAULT CHARSET=GBK |
+-------+-----------------------------------------------------------------------
------------------------- -----------------------------------------------------+
1 row in Set (0.00 sec)

2. The error occurs when there is data in the table, so changing the username character set does not succeed and requires emptying the data in the Users table

mysql> truncate table users;
Query OK, 3 rows affected (0.01 sec)

3. Re-change the character set of username in the user table

Mysql> ALTER TABLE users modify username char (a) character set GBK;
Query OK, 0 rows affected (0.06 sec)
records:0 duplicates:0 warnings:0

4. It is best to restart MySQL, and then you can insert Chinese

Mysql> INSERT into users values (88, ' Chinese ');
Query OK, 1 row affected (0.01 sec)

mysql> select * from users;
+--------+----------+
| userid | username
|  +--------+----------+
| 88 | chinese |
+--------+----------+
1 row in Set (0.00 sec)

Source: http://blog.csdn.net/coder_chang/article/details/54007143

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.