共計(jì) 4944 個(gè)字符,預(yù)計(jì)需要花費(fèi) 13 分鐘才能閱讀完成。
這篇文章給大家分享的是有關(guān) MySQL 中 BINARY 怎么用的內(nèi)容。丸趣 TV 小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨丸趣 TV 小編過(guò)來(lái)看看吧。
數(shù)據(jù)庫(kù)版本:
MySQL 5.6.26
線上某業(yè)務(wù)表為了區(qū)分大小寫(xiě),使用 BINARY 關(guān)鍵字,正常來(lái)說(shuō)使用這個(gè)關(guān)鍵字是走索引的,測(cè)試過(guò)程如下:
創(chuàng)建測(cè)試表,插入數(shù)據(jù):
drop table if EXISTS student;
CREATE TABLE `student` (
`id` int(11) PRIMARY key auto_increment,
`name` varchar(20) DEFAULT NULL,
key `idx_name`(`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
insert into `student` (`id`, `name`) values (1 , michael
insert into `student` (`id`, `name`) values (2 , lucy
insert into `student` (`id`, `name`) values (3 , nacy
insert into `student` (`id`, `name`) values (4 , mike
insert into `student` (`id`, `name`) values (null, guo
insert into `student` (`id`, `name`) values (6 , Guo
不加 BINARY 關(guān)鍵字可以走索引:
mysql desc select * from student where name = guo
+—-+————-+———+——+—————+———-+———+——-+——+————————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+———+——+—————+———-+———+——-+——+————————–+
| 1 | SIMPLE | student | ref | idx_name | idx_name | 63 | const | 2 | Using where; Using index |
+—-+————-+———+——+—————+———-+———+——-+——+————————–+
1 rows in set (0.03 sec)
正常來(lái)說(shuō) BINARY 關(guān)鍵字是可以走索引的:
mysql desc select * from student where BINARY name = guo
+—-+————-+———+——-+—————+———-+———+——+——+————————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+———+——-+—————+———-+———+——+——+————————–+
| 1 | SIMPLE | student | index | NULL | idx_name | 63 | NULL | 6 | Using where; Using index |
+—-+————-+———+——-+—————+———-+———+——+——+————————–+
1 rows in set (0.04 sec)
不使用 BINARY 關(guān)鍵字默認(rèn)不會(huì)區(qū)分大小寫(xiě):
mysql select * from student where name = guo
+—-+——+
| id | name |
+—-+——+
| 5 | guo |
| 6 | Guo |
+—-+——+
2 rows in set (0.03 sec)
mysql select * from student where name = Guo
+—-+——+
| id | name |
+—-+——+
| 5 | guo |
| 6 | Guo |
+—-+——+
2 rows in set (0.03 sec)
使用 BINARY 關(guān)鍵字可以區(qū)分大小寫(xiě):
mysql select * from student where BINARY name = guo
+—-+——+
| id | name |
+—-+——+
| 5 | guo |
+—-+——+
1 rows in set (0.04 sec)
mysql select * from student where BINARY name = Guo
+—-+——+
| id | name |
+—-+——+
| 6 | Guo |
+—-+——+
1 rows in set (0.03 sec)
mysql
到這里以上都沒(méi)問(wèn)題,但關(guān)鍵在于,業(yè)務(wù)的表結(jié)構(gòu)大于索引的最大長(zhǎng)度即字串長(zhǎng)度超過(guò) 255。
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(2000) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`(255))
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
mysql desc select * from student where name = guo
+—-+————-+———+——+—————+———-+———+——-+——+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+———+——+—————+———-+———+——-+——+————-+
| 1 | SIMPLE | student | ref | idx_name | idx_name | 768 | const | 2 | Using where |
+—-+————-+———+——+—————+———-+———+——-+——+————-+
1 rows in set (0.04 sec)
加上 BINARY 關(guān)鍵字不再走索引:
mysql desc select * from student where BINARY name = guo
+—-+————-+———+——+—————+——+———+——+——+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+———+——+—————+——+———+——+——+————-+
| 1 | SIMPLE | student | ALL | NULL | NULL | NULL | NULL | 6 | Using where |
+—-+————-+———+——+—————+——+———+——+——+————-+
1 rows in set (0.05 sec)
mysql
這時(shí)需要在表結(jié)構(gòu)里加上 BINARY
mysql ALTER TABLE student MODIFY COLUMN name VARCHAR(20) BINARY;
Query OK, 6 rows affected (0.06 sec)
數(shù)據(jù)庫(kù)會(huì)自動(dòng)轉(zhuǎn)換成 COLLATE utf8_bin
collate 關(guān)鍵字為校對(duì)集,主要是對(duì)字符集之間的比較和排序,可以通過(guò) show collation 查看所有的校對(duì)集
mysql show create table student\G
*************************** 1. row ***************************
Table : student
Create Table: CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
1 rows in set (0.39 sec)
mysql
mysql desc select * from student where name = guo
+—-+————-+———+——+—————+———-+———+——-+——+————————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+———+——+—————+———-+———+——-+——+————————–+
| 1 | SIMPLE | student | ref | idx_name | idx_name | 63 | const | 1 | Using where; Using index |
+—-+————-+———+——+—————+———-+———+——-+——+————————–+
1 rows in set (0.07 sec)
mysql
即可區(qū)分大小寫(xiě):
mysql select * from student where name = guo
+—-+——+
| id | name |
+—-+——+
| 5 | guo |
+—-+——+
1 rows in set (0.07 sec)
mysql select * from student where name = Guo
+—-+——+
| id | name |
+—-+——+
| 6 | Guo |
+—-+——+
1 rows in set (0.06 sec)
mysql
感謝各位的閱讀!關(guān)于“MySQL 中 BINARY 怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!