1.安装mysql时设置字符集
2.创建表时设置字符集
CREATE TABLE t_department (
sid varchar(32) NOT NULL,
pid varchar(32) NOT NULL,
thedata varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3.在表中更改字段字符集属性
mysql> show create table users;
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
| users | CREATE TABLE `users` (
`userid` int(11) default NULL,
`username` char(20) character set latin1 default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
1 row in set (0.00 sec)2.表中有数据时会报错,所以更改username字符集的操作不能成功,需要清空users表中的数据mysql> truncate table users;
Query OK, 3 rows affected (0.01 sec)3.重新更改user表中username的字符集mysql> alter table users modify username char(20) character set gbk;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 04.最好重启mysql,然后就可以插入中文mysql> insert into users values(88,'中文');
Query OK, 1 row affected (0.01 sec)
mysql> select * from users;
+--------+----------+
| userid | username |
+--------+----------+
| 88 | 中文 |
+--------+----------+
1 row in set (0.00 sec)
327



被折叠的 条评论
为什么被折叠?



