By default, MySQL searches are not case sensitive (although
there are some character sets that are never case insensitive,
such as czech). This means that if you
search with , you get all column values that start with
col_name LIKE
'a%'A or a. If you want to
make this search case sensitive, make sure that one of the
operands has a case sensitive or binary collation. For
example, if you are comparing a column and a string that both
have the latin1 character set, you can use
the COLLATE operator to cause either
operand to have the latin1_general_cs or
latin1_bin collation. For example:
col_nameCOLLATE latin1_general_cs LIKE 'a%'col_nameLIKE 'a%' COLLATE latin1_general_cscol_nameCOLLATE latin1_bin LIKE 'a%'col_nameLIKE 'a%' COLLATE latin1_bin
If you want a column always to be treated in case-sensitive
fashion, declare it with a case sensitive or binary collation.
See Section 13.1.8, “CREATE TABLE Syntax”.
Simple comparison operations (>=, >, =, <,
<=, sorting, and grouping) are based on each
character's “sort value.” Characters with the
same sort value (such as ‘E’,
‘e’, and
‘é’) are treated as the
same character.

User Comments
The expression STRCMP(col_name, "A") = 0 is case-sensitive
only before MySQL 4.0, for MySQL 4.0 or later use BINARY. See Manual: 6.3.2.1 String Comparison Functions
For MySQL 4.1 and later you can use binary collates:
create table dictionary (
ortography varchar(255) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
);
ALTER TABLE dictionary ADD UNIQUE INDEX (ortography);
Now, it's possible to insert as `ortography`:
São, sao, Sao, sAo, saO, SAO, etc...
for IN operation use binary
select * from table where binary field in (...);
interesting is that syntax
select * from table where field in (binary 'value1') doesn't give error message but doesn't work correct either.
Strings returned from DECODE are case sensitive for 4.0, probably because they're not really strings, they're blobs. However using "LIKE BINARY" keywords had no effect.
For example
SELECT COUNT(*) FROM EMAIL_TABLE WHERE DECODE(EMAIL, 'pass_phr') LIKE '%AOL.COM';
Will only yield the number of emails that have 'AOL.COM' at the end.
SELECT COUNT(*) FROM EMAIL_TABLE WHERE DECODE(EMAIL, 'pass_phr') LIKE '%aol.com';
will only yield the number of emails that have 'aol.com' at the end.
The same results were obtained using "LIKE BINARY" keyword. The email field was a tinyblob type.
I've found the Hex() function to be somewhat useful in case-sensitive searches.
SELECT * FROM mydatabase WHERE sendmail_id = BINARY 'k4912Nva000697'
(found set NvA000 vs Nva000)
w/o BINARY, both are found.
This allows me to do case-sensitive queries, without modifying the structure of the database. To make the query 'case sensitive' by default, use BINARY in the creation of the table.
http://dev.mysql.com/doc/refman/4.1/en/create-table.html
| VARCHAR(length) [BINARY]
<cut>
| LONGBLOB
| TINYTEXT [BINARY]
| TEXT [BINARY]
| MEDIUMTEXT [BINARY]
Finally, case-sensitive searches, if you can use them, are faster than case-insensitive (on unindexed fields).
Odd, I don't remember it working like this. In any case, I'm fairly sure that this used to work like:
WHERE `password` = 'pasSworD'
...would return nothing if the password was 'password' (lower-case). But I noticed that it accepts the above and returns the record back: case-insensitivety. Now, I use this to get it to work:
WHERE `password` COLLATE latin1_general_cs = 'pasSworD'
...which returns nothing if the password is 'password'. Perhaps it's the way my table is set up. Hope this helps anyone else who is confused about it.
Add your own comment.