Home Database Mysql Tutorial MYSQL查询大小写是否敏感问题分析_MySQL

MYSQL查询大小写是否敏感问题分析_MySQL

Jun 01, 2016 pm 01:12 PM
mysql database English alphabet

mysql数据库在做查询的时候,有时候是英文字母大小写敏感的,有时候又不是的,主要是由mysql的字符校验规则的设置决定的,通常默认是不支持的大小写字母敏感的。

 1. 什么是字符集和校验规则?

字符集是一套符号和编码。校对规则是在字符集内用于比较字符的一套规则。任何一个给定的字符集至少有一个校对规则,它可能有几个校对规则。要想列出一个字符集的校对规则,使用SHOW COLLATION语句。

1

校对规则一般有这些特征:

  • 两个不同的字符集不能有相同的校对规则。
  • 每个字符集有一个默认校对规则。例如,utf8默认校对规则是utf8_general_ci。
  • 存在校对规则命名约定:它们以其相关的字符集名开始,通常包括一个语言名,并且以_ci(大小写不敏感)、_cs(大小写敏感)或_bin(二元)结束。

2. 不同级别的字符集和校验规则可控制大小写敏感

MySQL5.1在同一台服务器、同一个数据库或甚至在同一个表中使用不同字符集或校对规则来混合定义字符串。字符集和校对规则有4个级别的默认设置:服务器级、数据库级、表级和连接级。

2.1服务器级

MySQL按照如下方法确定服务器字符集和服务器校对规则:

(1)修改配置文件/etc/my.cnf

在[mysqld]下添加:collation_server = utf8_bin

重启实例

2

更改服务器级的校验规则(collation_server )后,数据库校验规则(collation_collation)默认会继承服务器级的。

注意:

这个只适用于在重新启动之后, 新建的库,已存在的库不受影响.

同样的, 即使库的校验规则改了,已经存在的表不受修改影响;

同理与已经存在的列…

mysql> create database yutest0;Query OK, 1 row affected (0.00 sec)mysql> use yutest0;Database changedmysql> create table t1 (name varchar(10));Query OK, 0 rows affected (0.01 sec)mysql> insert into t1 values('AAA');Query OK, 1 row affected (0.00 sec)mysql> insert into t1 values('aaa');Query OK, 1 row affected (0.01 sec)mysql> select * from t1;+------+| name |+------+| AAA|| aaa|+------+2 rows in set (0.00 sec)mysql> select * from t1 where name='aaa';+------+| name |+------+| aaa|+------+1 row in set (0.00 sec)
Copy after login

可以看出,在服务器级进行相应的校对规则设置,查询大小写敏感。

(2)当服务器启动时根据有效的选项设置

当启动mysqld时,根据使用的初始选项设置来确定服务器字符集和校对规则。

shell> mysqld --character-set-server=latin1 --collation-server=latin1_swedish_ci
Copy after login

2.2数据库级

MySQL这样选择数据库字符集和数据库校对规则:

  • 如果指定了character set X和collate Y,那么采用字符集X和校对规则Y。
  • 如果指定了character set X而没有指定collate Y,那么采用character set X和character set X的默认校对规则。
  • 否则,采用服务器字符集和服务器校对规则。

(1)修改配置文件/etc/my.cnf

进行了两组测试:

1) 在[mysqld]下添加:

collation_server = utf8_bin

collation_database = utf8_bin

2) 在[mysqld]下添加:

collation_database = utf8_bin

重启实例,两组都不能正常启动,错误信息如下:

3

可见,my.cnf配置文件中不支持设置collation_database 变量。

(2)创建数据库时设置数据库校验规则

mysql> create database yutest default character set utf8 collate utf8_bin;Query OK, 1 row affected (0.00 sec)mysql> show variables like 'collation_%';+----------------------+-----------------+| Variable_name| Value |+----------------------+-----------------+| collation_connection | utf8_general_ci || collation_database | utf8_bin|| collation_server | utf8_general_ci |+----------------------+-----------------+3 rows in set (0.00 sec)mysql> select * from t1;+------+| name |+------+| ABC|| abc|+------+2 rows in set (0.00 sec)mysql> select * from t1 where name='abc';+------+| name |+------+| abc|+------+1 row in set (0.01 sec)
Copy after login

可以看出,在数据库级进行相应的校对规则设置,查询大小写敏感。

2.3表级

MySQL按照下面的方式选择表字符集和校对规则:

  • 如果指定了character set X和collate Y,那么采用character set X和collate Y。
  • 如果指定了character set X而没有指定collate Y,那么采用character set X和character set X的默认校对规则。
  • 否则,采用数据库字符集和服务器校对规则。

在创建表时设置表级校验规则:

mysql> create database yutest2;Query OK, 1 row affected (0.01 sec)mysql> use yutest2;Database changedmysql> create table t1(name varchar(10))	-> default character set utf8 collate utf8_bin;Query OK, 0 rows affected (0.01 sec)mysql> insert into t1 values('ABC');Query OK, 1 row affected (0.00 sec)mysql> insert into t1 values('abc');Query OK, 1 row affected (0.00 sec)mysql> show variables like 'collation_%';+----------------------+-----------------+| Variable_name		| Value		 |+----------------------+-----------------+| collation_connection | utf8_general_ci || collation_database | utf8_general_ci || collation_server	 | utf8_general_ci |+----------------------+-----------------+3 rows in set (0.00 sec)mysql> select * from t1;+------+| name |+------+| ABC|| abc|+------+2 rows in set (0.00 sec)mysql> select * from t1 where name='abc';+------+| name |+------+| abc|+------+1 row in set (0.00 sec)
Copy after login

可以看出,在表级进行相应的校对规则设置,查询大小写敏感。

2.4 连接级

考虑什么是一个“连接”:它是连接服务器时所作的事情。客户端发送SQL语句,例如查询,通过连接发送到服务器。服务器通过连接发送响应给客户端,例如结果集。对于客户端连接,这样会导致一些关于连接的字符集和校对规则的问题,这些问题均能够通过系统变量来解决:

mysql> show variables like 'character%';+--------------------------+----------------------------+| Variable_name| Value|+--------------------------+----------------------------+| character_set_client | utf8 || character_set_connection | utf8 || character_set_database | utf8 || character_set_filesystem | binary || character_set_results| utf8 || character_set_server | utf8 || character_set_system | utf8 || character_sets_dir | /usr/share/mysql/charsets/ |+--------------------------+----------------------------+8 rows in set (0.00 sec)
Copy after login
  • 当查询离开客户端后,在查询中使用哪种字符集?

服务器使用character_set_client变量作为客户端发送的查询中使用的字符集。

  • 服务器接收到查询后应该转换为哪种字符集?

转换时,服务器使用character_set_connection和collation_connection系统变量。它将客户端发送的查询从character_set_client系统变量转换到character_set_connection。

  • 服务器发送结果集或返回错误信息到客户端之前应该转换为哪种字符集?

character_set_results变量指示服务器返回查询结果到客户端使用的字符集。包括结果数据,例如列值和结果元数据(如列名)。

3. 创建数据库表时大小写不敏感,仍然有方法在查询时区分大小写

3.1 在SQL语句中使用collate

使用collate子句,能够为一个比较覆盖任何默认校对规则。collate可以用于多种SQL语句中,比如where,having,group by,order by,as,聚合函数。

mysql> select * from t1 where name collate utf8_bin = 'ABC';+------+| name |+------+| ABC |+------+1 row in set (0.00 sec)mysql> select * from t1 where name = 'ABC';+------+| name |+------+| ABC || Abc || abc |+------+3 rows in set (0.00 sec)mysql> select * from t1;+------+| name |+------+| ABC || Abc || abc |+------+3 rows in set (0.00 sec)
Copy after login

3.2 binary操作符

binary操作符是collate子句的一个速记符。binary ’x‘等价与’x‘ collate y,这里y是字符集’x‘二元校对规则的名字。每一个字符集有一个二元校对规则。例如,latin1字符集的二元校对规则是latin1_bin,因此,如果列a是字符集latin1,以下两个语句有相同效果:

select * from t1 order by binary a;select * from t1 order by a collate latin1_bin;
Copy after login
mysql> select * from t1 where binary name = 'ABC';+------+| name |+------+| ABC |+------+1 row in set (0.00 sec)mysql>mysql> select * from t1 where name = 'ABC';+------+| name |+------+| ABC || Abc || abc |+------+3 rows in set (0.00 sec)
Copy after login

参考链接:

MySQL5.1参考手册 http://dev.mysql.com/doc/refman/5.1/en/charset-server.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

RDS MySQL integration with Redshift zero ETL RDS MySQL integration with Redshift zero ETL Apr 08, 2025 pm 07:06 PM

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

How to fill in mysql username and password How to fill in mysql username and password Apr 08, 2025 pm 07:09 PM

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Apr 08, 2025 pm 07:12 PM

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

How to view mysql How to view mysql Apr 08, 2025 pm 07:21 PM

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;

Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

See all articles