mysql字符类型大小写敏感的讨论
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字符类型学习笔记
目录 一.字符集和字符编码 1.1.字符集 1.2.字符编码 二.字符集排序规则 2.1.排序规则定义 2.2 .排序规则特征 三.CHAR和VARCHAR 3.1.CHAR类型 3.2.VARCHA ...
- MySQL字符类型datetime与timestamp
这片博客来详细分区一下这哥俩! 首先来说明这两个字符类型: DATETIME 8 1000-01-01 00:00:00 ~9999~12-31 23:59:59 0000-00-00 00:00:0 ...
- mysql string types ---- mysql 字符类型详解
一.mysql 中包涵的字符类型: [national] char [(m)] [character set charset_name] [collate collation_name] [natio ...
- mysql字符类型
字符类型 #官网:https://dev.mysql.com/doc/refman/5.7/en/char.html #注意:char和varchar括号内的参数指的都是字符的长度 #char类型:定 ...
- MySQL 字符类型
字符类型 MySQL提供了多种关于字符存储的类型,但是在大多数情况下我们只使用char和varchar即可 类型 大小 用途 CHAR 0-255字节 定长字符串 VARCHAR 0-65535 字节 ...
- Mysql字符类型比较
一. binary和char比较: binary 字节为单位,char字符为单位,字符占几个字节取决于字符集 binary 比较规则基于字节值,char基于字符,即使是_bin的比较规则 范围都0- ...
- MYSQL字符类型数值排序
今天遇到MySQL数字排序问题,我的排序字段是经过计算后的,而计算后的字段直接拿来排序就会按照字符一个个排序,所以这里找到简单的方法, ORDER BY 排序字段* 或者 ORDER BY 排序字段+ ...
- mysql字符类型总结及常用字符函数
常用字符串函数: concat(s1,s2,s3..) 连接s1,s2,...sn为一个字符串 INSERT(str,x,y,instr)将字符串str从x位置开始,y个字符串替换为字符串 ...
- mysql数值类型总结及常用函数
最近在学习下,总结一下mysql数值类型: mysql字符类型分: 1.整数类型: 字节 值范围 INTERGER 1 ...
随机推荐
- mysql数据库导出excel xml等格式文件
http://jingyan.baidu.com/article/ac6a9a5e43a62e2b653eac83.html
- saltstack系列~第二篇
一 简介:今天咱们来继续学习saltstack 二 命名和分组 1 命名规则 1 ID构成 机房-DB类型-角色(主/从)-IP地址 2 分组构成 分为master slave两组即可 2 分组规则 ...
- np.mat()和np.transpose
例子: import numpy as np dataSet = [] with open('/home/lai/下载/20081023025304.plt') as fr: for line in ...
- 在Spring(4.3.22)中集成Hibernate(5.4.0)
(1)pom中添加相关依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibe ...
- java中网络设置代理
三种方式: 1.JVM启动时加参数设置代理 在系统启动时,使用-D项来设置代理. 例如: java -Dhttp.ProxyHost="proxyUrl" -Dhttp.Proxy ...
- 【vim】实时加密文本 ggVGg?
如果你不想让别人看懂你的屏幕上的内容,你可以使用一个内置的选项,通过下面的命令使用 ROT13 来对文本进行编码: ggVGg? gg 把光标移动到 Vim 缓冲区的第一行, V 进入可视模式, G ...
- 实现开发板与ubuntu的共享--根文件系统NFS--Samba共享【sky原创】
虚拟机要选择桥接,并且禁用有线和无线网卡,开启本地连接,本地连接属性要写如下: ip地址是在连上板子后,windows cmd 下 ipconfig得出的 板子的网线最好连接交换机或者 ...
- 转载:(Mac)在bash和zsh配置环境变量path的几种方法
参考文献 老习惯,列出本文参考或引用或转载的文档和博客,致以崇高的敬意,感兴趣的可以去看看 1.http://postgresapp.com/ 2.http://postgresapp.com/doc ...
- Ex 6_12 凸多边形的最优三角剖分..._第六次作业
假设顶点的总数为n,从0到n-1. 从序号为0的顶点开始以逆时针方向排序,对于 令子问题A[i,j]为包含顶点i,i+1, . . . j的凸多边形的最小三角剖分代价,dist(i,j)为顶点i到顶点 ...
- unicode-range特定字符使用font-face自定义字体
链接: https://www.zhangxinxu.com/wordpress/2016/11/css-unicode-range-character-font-face/