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 ...
随机推荐
- java字符串集合
一,java的接口跟C语言所能做到的相比确实是让人眼前一亮的东西.利用接口可以将多种东西放到一起,在编程过程中就能省略掉相同类的很多重复代码,将代码进行分类别的,统一的处理. 二,java中的字符串处 ...
- 集群下Dubbo负载均衡配置
在集群负载均衡时,Dubbo提供了4种均衡策略,默认为Random(随机调用) 负载均衡策略: 1).Random LoadBalance(随机,按照权重的设置随机概率) 2).RoundRobin ...
- Linux命令学习总结:date命令【转】
本文转自:http://www.cnblogs.com/kerrycode/p/3427617.html 命令简介: date 根据给定格式显示日期或设置系统日期时间.print or set the ...
- JS实现双击内容变为可编辑状态
在一些网站上我们经常看到交互性很强的功能.一些用户资料可以直接双击出现文本框,并在此输入新的资料即可修改,无需再按确定按钮等.. 我在网上查了很多资料,但都有一个小bug,就是当获取焦点后,光标的位置 ...
- C++编程命名规则
原文地址:http://www.cnblogs.com/ggjucheng/archive/2011/12/15/2289291.html 如果想要有效的管理一个稍微复杂一点的体系,针对其中事物的一套 ...
- zabbix3.0对tcp连接数和状态的监控优化
zabbix3.0对tcp连接数及状态的监控优化 之前对tcp的监控采用netstat命令,发现在服务器繁忙的时候效果不理想,这个命令占用大量的cpu有时候高达90%以上,可能会导致业务的不稳定,所以 ...
- centos6.5环境基于conga的web图形化界面方式配置rhcs集群
一.简介 RHCS 即 RedHat Cluster Suite ,中文意思即红帽集群套件.红帽集群套件(RedHat Cluter Suite, RHCS)是一套综合的软件组件,可以通过在部署时采用 ...
- jquery引入
网络地址:http://code.jquery.com/jquery-2.2.0.min.js 在需要的页面中直接使用网络地址,就不需要本地文件 <script type="text/ ...
- Android 工程中各种文件的介绍
一:Android.mk Android.mk 内部定义了一个或者多个源代码的模块,该文件的产生是和NDK相关的,NDK 是Android提供的一种工具,可以为编译mk文件内部的源代码提供依赖 ...
- 在Mac上安装GTK(go语言GUI)
1.在终端输入:xcode-select --install 安装command line工具, 如果安装了Xcode, 就直接跳过该步骤 2. 在终端输入:ruby -e "$(curl ...