饿虎岗资源网 Design By www.oxmxm.com
mysql字符类型默认是不区分大小写的,即select * from t where name='AAA'与='aaa'没区别,以下是测试的例子
(root@localhost)[hello]> create table test1(id int, name varchar(10)); (root@localhost)[hello]> insert into test1 values(1,'aaa'),(2,'AAA'),(3,'bbb'),(4,'BbB'); (root@localhost)[hello]> select * from test1; +------+------+ | id | name | +------+------+ | 1 | aaa | | 2 | AAA | | 3 | bbb | | 4 | BbB | +------+------+ (root@localhost)[hello]> select * from test1 where name = 'AAA'; +------+------+ | id | name | +------+------+ | 1 | aaa | | 2 | AAA | +------+------+ (root@localhost)[hello]> select * from test1 where name = 'aaa'; +------+------+ | id | name | +------+------+ | 1 | aaa | | 2 | AAA | +------+------+
可以看到此时where条件后面的'AAA'与'aaa',查出来的结果没啥区别。
如果只想找出'AAA'的可以有以下几种办法
1.在sql中加入binary关键字
(root@localhost)[hello]> select * from test1 where binary name = 'AAA'; +------+------+ | id | name | +------+------+ | 2 | AAA | +------+------+
2.修改列的定义
先查看原始表的定义
(root@localhost)[hello]> show create table test1\G *************************** 1. row *************************** Table: test1 Create Table: CREATE TABLE `test1` ( `id` int(11) DEFAULT NULL, `name` varchar(10) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
修改表test1的name列
alter table test1 modify column name varchar(10) character set utf8mb4 collate utf8mb4_bin default null;
collate utf8mb4_bin表示where过滤或者order by排序区分大小写
此时查看test1的定义
(root@localhost)[hello]> show create table test1\G *************************** 1. row *************************** Table: test1 Create Table: CREATE TABLE `test1` ( `id` int(11) DEFAULT NULL, `name` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
接着再执行查询语句
(root@localhost)[hello]> select * from test1 where name='AAA'; +------+------+ | id | name | +------+------+ | 2 | AAA | +------+------+
下面再创建一张test2表,就会发现上面修改列的语句其实相当于在创建表时varchar后面跟binary
(root@localhost)[hello]> create table test2(id int, name varchar(10) binary); (root@localhost)[hello]> show create table test2\G *************************** 1. row *************************** Table: test2 Create Table: CREATE TABLE `test2` ( `id` int(11) DEFAULT NULL, `name` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
下面介绍如何设置字符大小写敏感
- 数据库级别设置字符大小写敏感
创建
create database <db_name> default character set utf8mb4 collate utf8mb4_bin;
修改
alter database <db_name> default character set utf8mb4 collate utf8mb4_bin;
- 表级别设置字符大小写敏感
创建
create table <tb_name> ( ...... ) engine=innodb default charset=utf8mb4 collate=utf8mb4_bin;
修改
alter table <tb_name> engine=innodb default charset=utf8mb4 collate=utf8mb4_bin;
- 列级别设置字符大小写敏感
创建
create table <tb_name> ( `field1` varchar(10) character set utf8mb4 collate utf8mb4_bin, ...... )
修改
alter table <tb_name> modify column `field1` varchar(10) character set utf8mb4 collate utf8mb4_bin default null;
继承关系是列-->表-->库,优先级是列>表>库
以上就是MySQL 字符类型大小写敏感的详细内容,更多关于MySQL 字符类型大小写的资料请关注其它相关文章!
饿虎岗资源网 Design By www.oxmxm.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
饿虎岗资源网 Design By www.oxmxm.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 魏新雨《为你祈祷+新歌精逊2CD[WAV]
- 影心、明萨拉你选哪个?国外美女COS《博德3》
- 澳洲女子骑车跌入“自杀树”丛 疼痛持续9个月崩溃
- 育碧公布2024“她创力”计划:为女性游戏人提供辅导
- 刘美君.2017-千色·30总选3CD【环球】【WAV+CUE】
- 群星.2022-星河长明电视原声带【乐有奇思】【FLAC分轨】
- 陈淑桦.1995-淑桦盛开Forever【滚石】【WAV+CUE】
- 《再来一张》评测:出师成败皆系“赌”
- 《银河汉堡店》测评:我是银河走菜王!
- 《末日地带2》评测:酣畅淋漓的“和面”之旅
- 萧煌奇《没事的》[320K/MP3][96.22MB]
- 萧煌奇《没事的》[FLAC/分轨][263.38MB]
- 群星《音你而来 第6期》[320K/MP3][90.61MB]
- 交错战线爬塔攻略一览
- 战锤40K星际战士2全近战武器使用教学|近战武器连招表