≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 数据库 > MySQL

MySQL 字符类型大小写敏感的简单示例

人气:448 时间:2021-11-10

这篇文章主要为大家详细介绍了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 |
+------+------+

MySQL 字符类型大小写敏感

可以看到此时where条件后面的'AAA'与'aaa',查出来的结果没啥区别。

如果只想找出'AAA'的可以有以下几种办法
1.在sql中加入binary关键字

代码如下:


(root@localhost)[hello]> select * from test1 where binary name = 'AAA';
+------+------+
| id | name |
+------+------+
| 2 | AAA |
+------+------+

MySQL 字符类型大小写敏感

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

MySQL 字符类型大小写敏感

修改表test1的name列

代码如下:


alter table test1 modify column name varchar(10) character set utf8mb4 collate utf8mb4_bin default null;

MySQL 字符类型大小写敏感

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

MySQL 字符类型大小写敏感

接着再执行查询语句

代码如下:


(root@localhost)[hello]> select * from test1 where name='AAA';
+------+------+
| id | name |
+------+------+
| 2 | AAA |
+------+------+

MySQL 字符类型大小写敏感

下面再创建一张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

MySQL 字符类型大小写敏感

 

下面介绍如何设置字符大小写敏感

 

  • 数据库级别设置字符大小写敏感

创建

代码如下:


create database <db_name> default character set utf8mb4 collate utf8mb4_bin;

MySQL 字符类型大小写敏感

修改

代码如下:


alter database <db_name> default character set utf8mb4 collate utf8mb4_bin;

MySQL 字符类型大小写敏感

  • 表级别设置字符大小写敏感

创建

代码如下:


create table <tb_name> (
......
) engine=innodb default charset=utf8mb4 collate=utf8mb4_bin;

MySQL 字符类型大小写敏感

修改

代码如下:


alter table <tb_name> engine=innodb default charset=utf8mb4 collate=utf8mb4_bin;

MySQL 字符类型大小写敏感

  • 列级别设置字符大小写敏感

创建

代码如下:


create table <tb_name> (
`field1` varchar(10) character set utf8mb4 collate utf8mb4_bin,
......
)

MySQL 字符类型大小写敏感

修改

代码如下:


alter table <tb_name> modify column `field1` varchar(10) character set utf8mb4 collate utf8mb4_bin default null;

MySQL 字符类型大小写敏感

继承关系是列-->表-->库,优先级是列>表>库

以上就是MySQL 字符类型大小写敏感的详细内容,更多关于MySQL 字符类型大小写的资料请关注四海网其它相关文章!

本文来自:http://www.q1010.com/177/19738-0.html

注:关于MySQL 字符类型大小写敏感的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:字符类型

您可能感兴趣的文章

  • SQL Server实现二进制与字符类型之间的数据转换
上一篇:MySQL 的 21 个规范、优化最佳实践!
下一篇:MySQL COALESCE函数使用示例
热门文章
  • mysql 修改character_set_server为utf-8的简单示例
  • 解决MySQL丢失文件localhost.localdomain.pid、mysql.sock的示例
  • MySQL 数据类型binary和varbinary的简单示例
  • MySQL:reading initial communication packet问题解决方法
  • MySql 表类型MYISAM、InnoDB区别
  • bash: mysql: command not found 的解决方法
  • MYSQL默认用户名ROOT修改方法
  • MySQL 常用命令菜鸟教程
  • MySQL 使用命令行新建用户并授予权限
  • MySql 数据库物理文件存放位置查看示例
  • 最新文章
    • MySQL查看死锁与解除死锁的简单示例
    • MySQL 慢查询的功能实例
    • MySQL查看死锁与去除死锁的简单示例
    • MySQL找出未提交事务的SQL的简单示例
    • MySQL锁阻塞的的简单示例
    • MySQL中的binary类型使用操作的示例
    • SQL优化教程之in与range查询的简单示例
    • MySQL 的 21 个规范、优化最佳实践!
    • MySQL 字符类型大小写敏感的简单示例
    • 解决mybatis-plus分页传入参数后sql where条件没有limit分页信息的问题

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。