-- 创建数据库时,设置数据库的编码方式
-- CHARACTER SET:指定数据库采用的字符集,utf8不能写成utf-8
-- COLLATE:指定数据库字符集的排序规则,utf8的默认排序规则为utf8_general_ci(通过show character set查看)
drop
database
if EXISTS dbtest;
create
database
dbtest
CHARACTER
SET
utf8
COLLATE
utf8_general_ci;
-- 修改数据库编码alterdatabase dbtest CHARACTER SET GBK COLLATE gbk_chinese_ci;alter database dbtest CHARACTER SET utf8 COLLATEutf8_general_ci; |
-- 创建表时,设置表、字段编码use dbtest;droptableif exists tbtest;createtabletbtest(id int(10) auto_increment,user_name varchar(60) CHARACTERSET GBK COLLATEgbk_chinese_ci,email varchar(60),PRIMARYkey(id))CHARACTERSETutf8 COLLATEutf8_general_ci; |
-- modify table encoding alter < Code class= "SQL keyword" >table TBTEST&NBSP; Character set UTF8&NBSP; collate UTF8_GENERAL_CI; -- modify field encoding alter table TBTEST&NBSP; modify EMAIL&NBSP; varchar character set UTF8&NBSP; collate UTF8_GENERAL_CI; |
-- 查看所有的字符编码SHOW CHARACTERSET;-- 查看创建数据库的指令并查看数据库使用的编码show create databasedbtest; |
-- 查看数据库编码:show variables like‘%char%‘; |
-- 设置character_set_server、set character_set_client和set character_set_resultssetcharacter_set_server = utf8; -- 服务器的默认字符集。使用这个语句可以修改成功,但重启服务后会失效。根本的办法是修改配置MYSQL文件MY.INI, |
Character_set_server=utf8, configure it under the Mysqld field.
Set character_set_client = GBK; --the character set of the statement from the client. The server uses the Character_set_client variable as the character set used in queries sent by the client.
Set character_set_results = GBK; --the character set used to return query results to the client. The character_set_results variable instructs the server to return the query result to the character set used by the client. Includes result data, such as column values and result metadata, such as column names.
MySQL Database encoding settings