我们知道在pg中查询数据时,是区分大小写的。
如:
postgres=# select * from t_product;
id | name | currency_id
----+-----------------------+-------------
1 | PostgreSQL consulting | 1
(1 row)
postgres=# select * from t_product where name = 'PostgreSQL consulting';
id | name | currency_id
----+-----------------------+-------------
1 | PostgreSQL consulting | 1
(1 row)
postgres=# select * from t_product where name = 'PostgreSQL consultinG';
id | name | currency_id
----+------+-------------
(0 rows)
那么mysql查询是否区分大小写呢?
mysql的查询默认是不区分大小写的。
看个例子:
mysql> select * from test where name = 'A';
+------+------+-----------+------+
| id | name | phone | sex |
+------+------+-----------+------+
| 3 | A | 123456778 | 0 |
+------+------+-----------+------+
1 row in set (0.01 sec)
mysql> select * from test where name = 'a';
+------+------+-----------+------+
| id | name | phone | sex |
+------+------+-----------+------+
| 3 | A | 123456778 | 0 |
+------+------+-----------+------+
1 row in set (0.00 sec)
可以看到使用如下语句,查询到了同一条记录。
select * from test where name = 'A';
select * from test where name = 'a';
事实上,mysql的表查询大小写是否敏感,取决于表字段设置的collate规则。
我们看下这张表的charset和collate。
mysql> show create table test\G;
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
`sex` int(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> show table status like "test"\G;
*************************** 1. row ***************************
Name: test
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 3
Avg_row_length: 5461
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2022-06-24 14:48:36
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.01 sec)
ERROR:
No query specified
test表的charset是latin1,collate规则是latin1_swedish_ci。 _ci的含义是case inse nsitive,即不区分大小写。
我们可以通过show charset命令查看charset默认的排序规则,带_ci的都是大小写不敏感的。
如果我们想使查询区分大小写。有两种方法:
1、查询条件前binary;
mysql> select * from test where binary name = 'A';
+------+------+-----------+------+
| id | name | phone | sex |
+------+------+-----------+------+
| 3 | A | 123456778 | 0 |
+------+------+-----------+------+
1 row in set (0.00 sec)
mysql> select * from test where binary name = 'a';
Empty set (0.00 sec)
2、修改字段的collate;
比如charset为utf8的表,我们可以将它的collate设置为utf8_bin或utf8mb4_0900_as_cs。 _cs的含义是case sensitive,即大小写敏感。 _bin表示将字符串中的每一个字符用二进制数据存储,也是区分大小写的。
mysql> ALTER TABLE test MODIFY COLUMN name VARCHAR(25) CHARACTER SET utf8 COLLATE utf8_bin;
Query OK, 3 rows affected (0.10 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> show create table test\G;
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`id` int(11) DEFAULT NULL,
`name` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
`sex` int(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> select * from test where name = 'A';
+------+------+-----------+------+
| id | name | phone | sex |
+------+------+-----------+------+
| 3 | A | 123456778 | 0 |
+------+------+-----------+------+
1 row in set (0.00 sec)
mysql> select * from test where name = 'a';
Empty set (0.00 sec)
总结
pg中查询区分大小写,而mysql查询默认是不区分大小写的。mysql查询是否区分大小写主要跟字段设置的collate规则有关,_ci类型的排序规则,含义是case insensitive,即大小写不敏感的。_cs类型的排序规则,含义是case sensitive,即大小写敏感,此外_bin表示将字符串中的每一个字符用二进制数据存储,也是区分大小写的。
参考:https://blog.csdn.net/changerzhuo_319/article/details/96205174

点个“赞 or 在看” 你最好看!

👇👇👇咔片谢谢各位老板啦!!!




