在线精品99_中国九九盗摄偷拍偷看_91免费版在线观看_91.app_91高清视频在线_99热最新网站

MySQL中BINARY怎么用

120次阅读
没有评论

共计 4944 个字符,预计需要花费 13 分钟才能阅读完成。

这篇文章给大家分享的是有关 MySQL 中 BINARY 怎么用的内容。丸趣 TV 小编觉得挺实用的,因此分享给大家做个参考,一起跟随丸趣 TV 小编过来看看吧。

数据库版本:
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 怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-27发表,共计4944字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)
主站蜘蛛池模板: 欧美黑人巨大videos精品 | 国产精品欧美视频另类专区 | 成人在免费观看视频国产 | 国产女人被狂躁到高潮小说 | 久久成年人视频 | 中字幕视频在线永久在线观看免费 | 韩国电影午夜三级不卡 | 精品久久久久久亚洲综合网 | 特级a欧美做爰片毛片 | 99久久99久久 | 亚洲av永久无码精品网站 | 亚洲一区二区欧美日韩 | 97精品国产97久久久久久 | 少妇人妻大乳在线视频 | 真人午夜a一级毛片 | 国产国语特级一级aa毛片 | 动漫精品专区一区二区三区不卡 | 亚洲国产一区在线二区三区 | 天堂成人av| 最近的中文字幕在线看视频 | 亚洲精品91在线 | 国产亚洲精品久久久久婷婷瑜伽 | 国产成人精品自在线拍 | 国产欧美日韩综合精品二区 | 欧美人与动xxxxz0oz | 黄色视频在线免费观看 | 国产成人免费高清在线观看 | 国产精品久久久久不卡无毒 | 性免费网站 | 亚洲精品一区二区三区四区五区 | 骚片av蜜桃精品一区 | 色偷偷狠狠色综合网 | 免费无遮挡无码视频网站 | 2020亚洲欧美日韩在线观看 | 日韩亚洲av无码一区二区不卡 | 国产精品偷伦视频免费观看了 | 久久er精品热线免费 | 四虎最新永久免费视频 | 国产成人一级片 | 在线播放国产真实女同事 | 久久在线资源 |