• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    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 字符类型大小写的资料请关注脚本之家其它相关文章!

    您可能感兴趣的文章:
    • MySQL大小写敏感的注意事项
    • mysql如何设置不区分大小写
    • MySQL大小写敏感导致的问题分析
    • 解决MySQl查询不区分大小写的方法讲解
    • MySQL数据库大小写敏感的问题
    • mysql表名忽略大小写配置方法详解
    • Linux系统MySQL忘记密码,重置密码,忽略表名、列名大小写的方法
    • 详解MySQL查询时区分字符串中字母大小写的方法
    • MySql查询不区分大小写解决方案(两种)
    • MySQL库表名大小写的选择
    上一篇:MySQL 的 21 个规范、优化最佳实践!
    下一篇:MySQL 中行转列的方法
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    MySQL 字符类型大小写敏感 MySQL,字符,类型,大小写,