中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

MySQL中BINARY怎么用

發布時間:2021-11-02 17:32:52 來源:億速云 閱讀:2430 作者:小新 欄目:MySQL數據庫

這篇文章給大家分享的是有關MySQL中BINARY怎么用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

數據庫版本:
MySQL 5.6.26

線上某業務表為了區分大小寫,使用BINARY關鍵字,正常來說使用這個關鍵字是走索引的,測試過程如下:

創建測試表,插入數據:

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關鍵字可以走索引:

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)
正常來說BINARY關鍵字是可以走索引的:

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關鍵字默認不會區分大小寫:

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關鍵字可以區分大小寫:

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>
到這里以上都沒問題,但關鍵在于,業務的表結構大于索引的最大長度即字串長度超過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關鍵字不再走索引:

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>
這時需要在表結構里加上BINARY

mysql>ALTER TABLE student MODIFY COLUMN name VARCHAR(20) BINARY;
Query OK, 6 rows affected (0.06 sec)
數據庫會自動轉換成COLLATE utf8_bin
collate關鍵字為校對集,主要是對字符集之間的比較和排序,可以通過 show collation查看所有的校對集

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>
即可區分大小寫:

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>

感謝各位的閱讀!關于“MySQL中BINARY怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

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

AI

饶平县| 蒙阴县| 宾川县| 郑州市| 赤城县| 奉贤区| 那坡县| 宁明县| 太和县| 仙游县| 和龙市| 余干县| 阜新市| 沙湾县| 乾安县| 天门市| 伊宁县| 呼伦贝尔市| 拜城县| 德兴市| 栖霞市| 资溪县| 济南市| 邵阳市| 永兴县| 襄城县| 西华县| 宁都县| 泰和县| 秦皇岛市| 安溪县| 南安市| 北安市| 闵行区| 凌源市| 阜平县| 洛南县| 荔波县| 安龙县| 上饶县| 凤庆县|