ERROR 1366 (HY000): Incorrect string value: '\xE9\x83\x91\xE5\xB7\x9E' for column '
ERROR 1366 (HY000): Incorrect string value: '\xE9\x83\x91\xE5\xB7\x9E' for column 'aa' at row 1
建立表之後不能插入中文字元?為啥呢?
瞭解字符集的重要性。它必須在建庫之前要確定好,恢復備份時也需要注意
mysql> use test
Database changed
mysql> create table zgy(aa char(20));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into zgy values('zgzg');
Query OK, 1 row affected (0.01 sec)
mysql> insert into zgy values('鄭州');
ERROR 1366 (HY000): Incorrect string value: '\xE9\x83\x91\xE5\xB7\x9E' for column 'aa' at row 1
mysql> show variables like '%char%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
這部分已執行
set character_set_client=utf8;
set character_set_connection=utf8;
set character_set_database=utf8;
set character_set_results=utf8;
set character_set_server=utf8;
set character_set_system=utf8;
set collation_connection=utf8;
set collation_database=utf8;
set collation_server=utf8;
set character_set_server=utf8;
mysql> show variables like'%char%';
+--------------------------+----------------------------------+
| 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 | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
仍然報錯?
mysql> insert into zgy values('鄭州');
ERROR 1366 (HY000): Incorrect string value: '\xE9\x83\x91\xE5\xB7\x9E' for column 'aa' at row 1
這時候只是將DB的改成utf8了,而zgy表是之前建立的
mysql> alter database test character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> show create database test;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table zgy;
+-------+------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------+
| zgy | CREATE TABLE `zgy` (
`aa` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
改變表的字符集,發現還不行
mysql> alter table zgy character set utf8;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into zgy values('鄭州');
ERROR 1366 (HY000): Incorrect string value: '\xE9\x83\x91\xE5\xB7\x9E' for column 'aa' at row 1
mysql>
mysql> show create table zgy;
+-------+-------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------+
| zgy | CREATE TABLE `zgy` (
`aa` char(20) CHARACTER SET latin1 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
修改欄位的字符集
建立庫時預設已經指定了表和欄位等物件的字符集
mysql> alter table zgy aa char(20) CHARACTER SET utf8;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'aa char(20) CHARACTER SET utf8' at line 1
mysql> ALTER TABLE zgy CHANGE aa aa char(20) CHARACTER SET utf8;
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> show create table zgy;
+-------+----------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------+
| zgy | CREATE TABLE `zgy` (
`aa` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into zgy values('鄭州');
Query OK, 1 row affected (0.01 sec)
mysql> insert into zgy values('深圳');
Query OK, 1 row affected (0.00 sec)
mysql> select * from zgy;
+--------------+
| aa |
+--------------+
| zgzg |
| asasas121212 |
| 鄭州 |
| 深圳 |
+--------------+
4 rows in set (0.00 sec)
小常識:
看到是gbk和latin1說明不支援中文,所以需要修改字元型別
在latin1中,每個字元只有一個位元組長。在utf8中,一個字元可以由多個位元組組成。因此,utf8具有比latin1更多的字元(並且它們具有的字元不一定由相同的位元組/位元組序列表示)。但是我們的中文漢字是要2個位元組還是3個位元組哦,所有latin1就不能儲存漢字啦。
優點:一勞永逸,在建立資料庫時就設定為utf8格式,以後再次資料庫下建立表時以及新增表資料時都不用擔心中文字元問題。
CREATE database testdb DEFAULT CHARACTER SET utf8;
一些關於檢視和修改字符集的MySQL知識:
檢視mysql的字符集:show variables where Variable_name like '%char%';
檢視某一個數據庫字符集:show create database enterprises;(注:enterprises為資料庫)
檢視某一個數據表字符集:show create table employees;(注:employees為資料表)
修改mysql的字符集
set character_set_client=utf8;
set character_set_connection=utf8;
set character_set_database=utf8;
set character_set_results=utf8;
set character_set_server=utf8;
set character_set_system=utf8;
set collation_connection=utf8;
set collation_database=utf8;
set collation_server=utf8;
修改資料庫enterprises的字符集:
alter database enterprises character set utf8
修改資料表employees的字符集:
alter table employees character set utf8
修改欄位的字符集
alter table employees change name name char(10) character set utf-8;