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 gc
mark下来 https://plumbr.eu/handbook/what-is-garbage-collection
- Java用System读取系统相关信息、环境变量——(六)
package Java_Test; public class System1 { public static void main(String[] args) { // TODO Auto-gene ...
- Jetson tk1 安装 usbtoserials 驱动(重新刷机)
一.tk1驱动包,文件系统和源码下载 截止2016年9月1号,tk1最版本为R21.5. (以下三个文件放在同一个文件夹下) 1.driver package(驱动包,相当于安装程序) https:/ ...
- Win10 + Visual Studio 2017 下 OpenCV无法显示图像的问题
测试代码如下: #include "stdafx.h" #include<opencv2\opencv.hpp> #include<opencv2\highgui ...
- oracle 存储过程 clob 字段 调试
clob 没法直接赋值调试,可以新建一个存储过程,赋值给clob字段,然后调试
- mysql通过centos本地命令行还原数据库出现乱码问题
将sql文件上传到centos系统中,还原mysql数据库,发现是乱码 mysql -h10.11.8.62 -uroot -p dbtest </data/dbsql/dbtest.sql 数 ...
- JS排序算法之快速排序
const Arr = [85, 24, 63, 45, 17, 31, 96, 50]; function quickSort(arr) { 80 if (arr.length <= 1) { ...
- HTTP协议 (1)
HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议. HTT ...
- Guice 依赖绑定
Guice 依赖绑定 连接绑定(Linked Bingdings) 连接绑定是 Guice 最基本的一种绑定方式.这种绑定方式我们需要在自己定义的 Module 的 configure() 中编写绑定 ...
- 性能测试三十三:监控之Linux系统监控命令大全
1.top命令top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top - 01:06:48 up 1: ...