MySQL字段值如何区分大小写
注意:设置的是
字段值
区分大小写
1. 查询时指定大小写敏感,加关键字‘BINARY’
(1)删表,建表,新增数据
drop table binary_test;
CREATE TABLE binary_test (
`id` INT unsigned PRIMARY key NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into binary_test (name) values ('QWERTY');
insert into binary_test (name) values ('qwerty');
(2)测试数据
①查询小写字母
select * from binary_test where name ='qwerty';
②查询大写字母
select * from binary_test where binary name ='QWERTY';
2. 定义表结构时指定字段大小写敏感
关键字“BINARY
”指定guid
字段大小写敏感
drop table binary_test;
CREATE TABLE binary_test (
`id` INT unsigned PRIMARY key NOT NULL AUTO_INCREMENT,
`name` varchar(255) BINARY NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into binary_test (name) values ('QWERTY');
insert into binary_test (name) values ('qwerty');
注意 name字段使用binary修饰了。
select * from binary_test where name ='qwerty';
3. 修改排序规则(COLLATION)
show variables like ‘collation_database’;
Collation
以 “_ci
"结尾的不区分大小写(ci——Case Ignore),以”_bin
"或者"_cs
"结尾的区分大小写
将Collation
改为 utf8_bin
(大小写敏感的)
可以为库、表、列指定Collation。
优先级为 列>表>库
CREATE DATABASE
sys_user
COLLATE utf8_bin;ALTER TABLE
sys_user
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_bin;ALTER TABLE
sys_user
MODIFY COLUMNusername
varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
ALTER TABLE
sys_user
MODIFY COLUMNusername
VARCHAR(255) BINARY