溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL數(shù)據(jù)庫中字符的類型是否區(qū)分大小寫

發(fā)布時間:2020-12-07 15:15:30 來源:億速云 閱讀:468 作者:Leah 欄目:開發(fā)技術

這篇文章給大家介紹MySQL數(shù)據(jù)庫中字符的類型是否區(qū)分大小寫,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

mysql字符類型默認是不區(qū)分大小寫的,即select * from t where name='AAA'與='aaa'沒區(qū)別,以下是測試的例子

(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',查出來的結果沒啥區(qū)別。

如果只想找出'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排序區(qū)分大小寫

此時查看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í)行查詢語句

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

下面再創(chuàng)建一張test2表,就會發(fā)現(xiàn)上面修改列的語句其實相當于在創(chuàng)建表時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ù)據(jù)庫級別設置字符大小寫敏感

創(chuàng)建

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

修改

alter database <db_name> default character set utf8mb4 collate utf8mb4_bin;
  • 表級別設置字符大小寫敏感

創(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;
  • 列級別設置字符大小寫敏感

創(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;

繼承關系是列-->表-->庫,優(yōu)先級是列>表>庫

關于MySQL數(shù)據(jù)庫中字符的類型是否區(qū)分大小寫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI