Implement case sensitivity in MySQL SELECT statements


SELECT is by default case-insensitive. For case-sensitive implementation, the BINARY operator is used. Following is the syntax :

select *from yourTableName where BINARY yourColumnName=yourValue;

Let us first create a table −

mysql> create table DemoTable
(
   Name varchar(40)
);
Query OK, 0 rows affected (0.74 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('CHRIS');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('chris');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('CHris');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Name  |
+-------+
| Chris |
| CHRIS |
| chris |
| CHris |
+-------+
4 rows in set (0.00 sec)

Following is the query for case-sensitive select −

mysql> select *from DemoTable where BINARY Name='Chris';

This will produce the following output −

+-------+
| Name  |
+-------+
| Chris |
+-------+
1 row in set (0.04 sec)

Updated on: 07-Oct-2019

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements