mysql修改表、字段、库的字符集

时间:2022-04-17 14:54:07

mysql修改表、字段、库的字符集

把表默认的字符集和所有字符列(CHAR,VARCHAR,TEXT)改为新的字符集:


ALTER TABLE tbl_name CONVERT TO CHARACTER SET character_name [COLLATE ...]
如:ALTER TABLE logtest CONVERT TO CHARACTER SET gbk COLLATE gbk_chinese_ci;

修改字段的字符集:


ALTER TABLE tbl_name CHANGE c_name c_name CHARACTER SET character_name [COLLATE ...];
如:ALTER TABLE logtest CHANGE title title VARCHAR(100) CHARACTER SET gbk COLLATE gbk_chinese_ci;


修改数据库字符集:


ALTER DATABASE db_name DEFAULT CHARACTER SET character_name [COLLATE ...];

只是修改表的默认字符集:


ALTER TABLE tbl_name DEFAULT CHARACTER SET character_name [COLLATE...];
如:ALTER TABLE logtest DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

以上CLOLATE 表明该表、字段、数据库 校对、整理所采用的字符集,如在字段上使用COLLATE gbk_chinese_ci,则该字段ORDER BY 排序时会使用拼音去排序结果,即使该字段是按照UTF8编码。