MySQL 字符類型大小寫敏感
mysql字符類型默認(rèn)是不區(qū)分大小寫的,即select * from t where name='AAA'與='aaa'沒區(qū)別,以下是測(cè)試的例子
(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 | +------+------+
可以看到此時(shí)where條件后面的'AAA'與'aaa',查出來的結(jié)果沒啥區(qū)別。
如果只想找出'AAA'的可以有以下幾種辦法
1.在sql中加入binary關(guān)鍵字
(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排序區(qū)分大小寫
此時(shí)查看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
接著再執(zhí)行查詢語(yǔ)句
(root@localhost)[hello]> select * from test1 where name='AAA'; +------+------+ | id | name | +------+------+ | 2 | AAA | +------+------+
下面再創(chuàng)建一張test2表,就會(huì)發(fā)現(xiàn)上面修改列的語(yǔ)句其實(shí)相當(dāng)于在創(chuàng)建表時(shí)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
下面介紹如何設(shè)置字符大小寫敏感
- 數(shù)據(jù)庫(kù)級(jí)別設(shè)置字符大小寫敏感
創(chuàng)建
create database <db_name> default character set utf8mb4 collate utf8mb4_bin;
修改
alter database <db_name> default character set utf8mb4 collate utf8mb4_bin;
- 表級(jí)別設(shè)置字符大小寫敏感
創(chuàng)建
create table <tb_name> ( ...... ) engine=innodb default charset=utf8mb4 collate=utf8mb4_bin;
修改
alter table <tb_name> engine=innodb default charset=utf8mb4 collate=utf8mb4_bin;
- 列級(jí)別設(shè)置字符大小寫敏感
創(chuàng)建
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;
繼承關(guān)系是列-->表-->庫(kù),優(yōu)先級(jí)是列>表>庫(kù)
以上就是MySQL 字符類型大小寫敏感的詳細(xì)內(nèi)容,更多關(guān)于MySQL 字符類型大小寫的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MySQL一次性創(chuàng)建表格存儲(chǔ)過程實(shí)戰(zhàn)
這篇文章主要介紹了MySQL一次性創(chuàng)建表格存儲(chǔ)過程實(shí)戰(zhàn),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07mysql 5.6.14主從復(fù)制(也稱mysql AB復(fù)制)環(huán)境配置方法
這篇文章主要介紹了mysql 5.6.14主從復(fù)制(也稱mysql AB復(fù)制)環(huán)境配置方法,需要的朋友可以參考下2016-04-04Centos6.5在線安裝mysql 8.0詳細(xì)教程
這篇文章主要為大家介紹了Centos6.5在線安裝 mysql 8.0詳細(xì)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11mysql-canal-rabbitmq 安裝部署超詳細(xì)教程
這篇文章主要介紹了mysql-canal-rabbitmq 安裝部署超詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03MySQL 在創(chuàng)建和刪除用戶時(shí)出現(xiàn)的ERROR 1396 (HY000)錯(cuò)誤問題解決
MySQL作為流行的數(shù)據(jù)庫(kù)系統(tǒng),涉及用戶管理時(shí)可能遇到ERROR1396錯(cuò)誤,該錯(cuò)誤發(fā)生在嘗試創(chuàng)建已存在的用戶或刪除不存在的用戶時(shí),解決方法包括檢查用戶存在性或選擇不同用戶名,此外,MySQL提供了創(chuàng)建和授權(quán)用戶的便捷工具,注意使用FLUSH PRIVILEGES命令使授權(quán)生效2024-09-09