--显示数据库反对的字符集

show char set;

1.针对每一种申明反对的字符集(反对的字符集该当蕴含utf8),创立一张表,并测试:

--依据show char set 执行的后果 选如下字符集创立表

CREATE TABLE test (
id int auto_increment primary key,
name binary(16),
gbk varchar(2) CHARACTER SET gbk DEFAULT NULL,
utf8 varchar(2) CHARACTER SET utf8 DEFAULT NULL,
latin_utf8 varchar(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
a)测试增删改查操作;

insert into test (name,gbk,utf8,latin_utf8) values ('liudehua','ab','cd','df');
insert into test (name,gbk,utf8,latin_utf8) values ('liudehua','ab','cd','df');
insert into test (name,gbk,utf8,latin_utf8) values ('zhangxueyou','ef','c1','ji');


delete from test where latin_utf8='ji';

select * from test;
idnamegbkutf8latin_utf8
1liudehuaabcddf
2liudehuaabcddf


update test set utf8='中国' where id = 2 and utf8='cd';

select * from test;
idnamegbkutf8latin_utf8
1liudehuaabcddf
2liudehuaab中国df

b)对于反对utf8、gbk等含中文的状况,测试汉字“中国”增删改查操作;

insert into test (name,gbk,utf8,latin_utf8) values ('中国','中国','中国',_latin1'中国');
insert into test (name,gbk,utf8,latin_utf8) values ('中国','中国','中国',_latin1'中国');
delete from test where utf8='中国' and id =4;
update test set utf8='日本',gbk='日本' where gbk='中国' and utf8='中国';

select * from test;
idnamegbkutf8latin_utf8
1liudehua日本日本df
5中国日本日本中国

c)对于反对查问二进制值的状况,测试查问汉字“中国”的编码值,确定以正确的字符集存储;

select hex(name),hex(gbk),hex(utf8),hex(latin_utf8) from test;
hex(name)hex(gbk)hex(utf8)hex(latin_utf8)
6C697564656875610000000000000000C8D5B1BEE697A5E69CAC6466
E4B8ADE59BBD00000000000000000000C8D5B1BEE697A5E69CACE4B8ADE59BBD
E4B8ADE59BBD00000000000000000000D6D0B9FAE4B8ADE59BBDE4B8ADE59BBD

2.针对申明反对两种或更多字符集的状况,测试:

a)如果申明反对存储字符集转换,在反对的范畴内测试存储字符集转换;

set names latin1;

show variables like 'character_set%';
Variable_nameValue
character_set_clientlatin1
character_set_connectionlatin1
character_set_databaseutf8
character_set_filesystembinary
character_set_resultslatin1
character_set_serverutf8mb4
character_set_systemutf8
select * from test;
idnamegbkutf8latin_utf8
1liudehua????df
5中国????中国
6中国????中国

b)如果申明反对连贯字符集和存储字符集不统一,测试连贯字符集与存储字符集不雷同时,数据增、删、改、查操作是否正当解决,连贯字符集与存储字符集都反对的汉字在增、删、改、查时是否正确转换;

MySQL [test]> set session character_set_connection=latin1;

MySQL [test]> select * From test;
idnamegbkutf8latin_utf8
1liudehua日本日本df
5中国??日本中国
6中国中国中国中国


update test set gbk='美国' WHERE id=6;

MySQL [test]> select * from test;
idnamegbkutf8latin_utf8
1liudehua日本日本df
5中国??日本中国
6中国??中国中国

3.如果申明反对二进制字符串,测试插入和查问出的二进制字符串的二进制数值是否完全一致。

insert into test (name,gbk,utf8,latin_utf8) values (UNHEX('4D7953514C'),'中国','中国',_latin1'中国');
select * from test;

idnamegbkutf8latin_utf8
1liudehua日本日本df
5中国??日本中国
6中国??中国中国
7MySQL中国中国中国
select hex(name) from test where id = 7;
hex(name)
4D7953514C0000000000000000000000