举个例子,我创建表时没有设定字符集,默认是latin1,后来发现中文全乱码,就改成gbk。新插入的数据就正常了,但是已有的数据还是乱码,怎么让已有数据也恢复正常。下面是我的一个例子:
mysql> show create table example2 \G;
*************************** 1. row ***************************
Table: example2
Create Table: CREATE TABLE example2
(Id
int(11) NOT NULL AUTO_INCREMENT,a
varchar(20) DEFAULT NULL,b
date DEFAULT NULL,
PRIMARY KEY (Id
)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> ALTER TABLE example2 CONVERT TO CHARACTER SET gbk;
Query OK, 1 row affected (1.49 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from example2;
+----+------+------------+
| Id | a | b |
+----+------+------------+
| 1 | ?? | 2014-07-24 |
+----+------+------------+
1 row in set (0.00 sec)
mysql> insert example2 values(2,'我',now());
Query OK, 1 row affected, 1 warning (0.08 sec)
mysql> select * from example2;
+----+------+------------+
| Id | a | b |
+----+------+------------+
| 1 | ?? | 2014-07-24 |
| 2 | 我 | 2014-07-24 |
+----+------+------------+
2 rows in set (0.00 sec)
mysql>