IT Community - Software Programming, Web Development and Technical Support

Handle Case Sensitive Search in MySQL

This is a discussion on Handle Case Sensitive Search in MySQL within the Database Support forums, part of the Web Development category; This short note requires some basic knowledge of MySQL Character Sets & Collation. If you configured your mysql server Default ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 05-11-2007, 04:43 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Handle Case Sensitive Search in MySQL

This short note requires some basic knowledge of MySQL Character Sets & Collation.

If you configured your mysql server Default Character set with LATIN1, then
default collation for that Character Set LATIN1 is LATIN1_SWEDISH_CI (Where CI denotes Case Insensitive).

Consider the below given example :

mysql> create table test( sno integer, name varchar(15) ); -- default character set will be used by the system.
mysql> insert into test values (1, 'xyz');
mysql> insert into test values (2, 'XYZ');
mysql> insert into test values (3, 'Xyz');
mysql> insert into test values (4, 'XYz');
mysql> insert into test values (5, 'xyZ');

mysql> select * from test where name = 'xyz'; -- this query will result all the five values.
+------+------+
| sno | name |
+------+------+
| 1 | xyz |
| 2 | XYZ |
| 3 | Xyz |
| 4 | XYz |
| 5 | xyZ |
+------+------+
5 rows in set (0.00 sec)

If we want to do case sensitive search then we need specify the charset collation to be CI - CASE SENSITIVE.
You can find the case sensitive collation by issuing

mysql> show collation;
mysql> show collation where charset = 'latin1';

'latin1_general_cs' is the case sensitive collation for character set 'latin1'

Now, drop the table and create again and specify the character set and collation as given below.

mysql> drop table if exists test;
mysql> create table test( sno integer, name varchar(15)) CHARACTER SET latin1 COLLATE latin1_general_cs;;
mysql> insert into test values (1, 'xyz');
mysql> insert into test values (2, 'XYZ');
mysql> insert into test values (3, 'Xyz');
mysql> insert into test values (4, 'XYz');
mysql> insert into test values (5, 'xyZ');
mysql> select * from test where name = 'xyz';
+------+------+
| sno | name |
+------+------+
| 1 | xyz |
+------+------+

Instead of drop & creating table once again, we can use alter statement to modify the character and collation.

mysql> ALTER TABLE test CHARACTER SET latin1 COLLATE latin1_general_ci;

This is the way we can achieve case sensitive select / search in MySQL.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-13-2007, 10:57 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Handle Case Sensitive Search in MySQL

hi,
this is really fantastic articles.
is there any possibility to set this without altering or creating new table?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-14-2007, 06:21 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: Handle Case Sensitive Search in MySQL

Dear jayseel,

Without changing the Character / Collation type it's not possible.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-16-2007, 12:45 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Handle Case Sensitive Search in MySQL

Hi Prakash,

ok Good. Nice 2 see u again.

can u pls tell me what are the characters type other than latin1 available as like this?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-22-2007, 10:54 PM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: Handle Case Sensitive Search in MySQL

Hi,

You can find the list of character set available in mysql by issuing the following show commnad.

SHOW CHARSET; -- this will list available character set in format given below.

----------------------------------------------------------------------
Charset Description Default collation Maxlen
----------------------------------------------------------------------
8-Dec DEC West European dec8_swedish_ci 1
latin1 cp1252 West European latin1_swedish_ci 1
latin2 ISO 8859-2 Central European latin2_general_ci 1
latin5 ISO 8859-9 Turkish latin5_turkish_ci 1
utf8 UTF-8 Unicode utf8_general_ci 3
binary Binary pseudo charset binary 1
geostd8 GEOSTD8 Georgian geostd8_general_ci 1
...
...
----------------------------------------------------------------------

If you want to list out the all the collations for each character set then try the following command.

SHOW COLLATION;
SHOW COLLATION WHERE Charset LIKE 'latin1%';
(like this you can find all the collation list for any available character set in mysql)
----------------------------------------------------------------------
Collation Charset Id Default Compiled Sortlen
----------------------------------------------------------------------
latin1_german1_ci latin1 5 Yes 1
latin1_swedish_ci latin1 8 Yes Yes 1
latin1_danish_ci latin1 15 Yes 1
latin1_german2_ci latin1 31 Yes 2
latin1_bin latin1 47 Yes 1
latin1_general_ci latin1 48 Yes 1
latin1_general_cs latin1 49 Yes 1
latin1_spanish_ci latin1 94 Yes 1
----------------------------------------------------------------------

-- Priyan..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-05-2007, 11:40 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Handle Case Sensitive Search in MySQL

Hi Priyan/Jayaseelan,

Its really a wonderful explanation regarding the case sensitive search using alter table converting the field characterset and collation - By Priyan.

But can i tell even a very simple way to do that without changing the field Character set and Collation?

Try this one ....


mysql> DROP TABLE IF EXISTS test;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE test(sno INTEGER,name VARCHAR(15) );
Query OK, 0 rows affected (0.06 sec)

mysql> DESC test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sno | int(11) | YES | | NULL | |
| name | varchar(15) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO test VALUES (1, 'xyz');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO test VALUES (2, 'XYZ');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO test VALUES (3, 'Xyz');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO test VALUES (4, 'XYz');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO test VALUES (5, 'xyZ');
Query OK, 1 row affected (0.00 sec)

mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM test WHERE name = 'xyz';
+------+------+
| sno | name |
+------+------+
| 1 | xyz |
| 2 | XYZ |
| 3 | Xyz |
| 4 | XYz |
| 5 | xyZ |
+------+------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM test WHERE HEX(name) = HEX('xyZ');
+------+------+
| sno | name |
+------+------+
| 5 | xyZ |
+------+------+
1 row in set (0.00 sec)

Just converting the String Value to Hexa Decimal Value, We have get the Result of the Exact String

Cool .....
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-10-2007, 07:21 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: Handle Case Sensitive Search in MySQL

Hi Murali,
There is another way to achieve the case sensitive search in mysql using a single keyword BINARY.

Considering the example given earlier, just use the query given below without bothering about what character set / collation and all.

mysql> SELECT * FROM test WHERE BINARY name = 'xyZ';
+------+------+
| sno | name |
+------+------+
| 5 | xyZ |
+------+------+
1 row in set (0.00 sec)
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compare the string with case sensitive in sql server? krishnakumar Database Support 9 10-10-2008 04:54 AM
Current and Future Search Trends: What the Top Internet Search Engines Are Doing vijayanand Technology BUZZzzzzz 1 02-01-2008 10:45 AM
Case sensitive comparison in mysql? velhari Database Support 1 12-04-2007 01:29 AM
search engines in MySql vijayanand Database Support 0 09-05-2007 08:52 AM
XML case-sensitive vigneshgets C# Programming 2 07-12-2007 05:18 AM


All times are GMT -7. The time now is 01:25 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0