有表A,创建时的编码为utf8,现在改成gbk,执行ALTER TABLE A DEFAULT CHARACTER SET gbk;
然后查看表的编码: SHOW CREATE TABLE A;
CREATE TABLE `A` (
`username` char(10) CHARACTER SET utf8 DEFAULT NOT NULL,
`password` varchar(32) CHARACTER SET utf8 NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
问一下怎么样修改表编码的同时将字段的编码也一并修改
然后查看表的编码: SHOW CREATE TABLE A;
CREATE TABLE `A` (
`username` char(10) CHARACTER SET utf8 DEFAULT NOT NULL,
`password` varchar(32) CHARACTER SET utf8 NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
问一下怎么样修改表编码的同时将字段的编码也一并修改
解决方案
40
没那么麻烦,一句SQL搞定.这也是各DBMS有些不太相同的地方.
mysql> CREATE TABLE `A` ( -> `username` char(10) CHARACTER SET utf8 NOT NULL, -> `password` varchar(32) CHARACTER SET utf8 NOT NULL -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.01 sec) mysql> mysql> ALTER TABLE `A` DEFAULT CHARACTER SET gbk , -> modify `username` char(10) CHARACTER SET gbk NOT NULL, -> modify `password` varchar(32) CHARACTER SET gbk NOT NULL; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table `A`; +--+--+ | Table | Create Table | +--+--+ | A | CREATE TABLE `a` ( `username` char(10) NOT NULL, `password` varchar(32) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=gbk | +--+--+ 1 row in set (0.00 sec) mysql>
其实官方文档上也都有参考.看明白了就行.