Wednesday 18 July 2018

MySQL: Implementing case-sensitive functionality in query

MySQL: Implementing case-sensitive functionality in query

Like SQL Server , in MySQL also we can check case-sensitivity inside a query. For this we have to use “BINARY” keyword.
 
Lets say I have a Users Table having a column named as “FirstName” ofVarchar DataType. 
Below are the records present inside this table.
Id        FirstName        LastName      JobTitle 
1         Albert             Roger       Software Engg
2         albert             Joseph      n/w Engg
3         Bob                Bette       Software Engg
If I write the query as :-
SELECT * FROM users WHERE FirstName = ‘Albert’;
The query will return 2 rows
Id       FirstName         LastName        JobTitle 1        Albert              Roger         Software Engg
2        albert              Joseph        n/w Engg
To get only that record whose FirstName is “Albert”.
We have to write the query as :- 
SELECT * FROM users WHERE BINARY FirstName = ‘Albert’;
This query will return
Id       FirstName         LastName        JobTitle 1        Albert              Roger         Software Engg
We can also declare a particular column as BINARY by using following query :- 
ALTER TABLE `users` MODIFY COLUMN `FirstName` VARCHAR(100) BINARY DEFAULT NULL;

0 comments:

Post a Comment