LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2010, 04:26 AM   #1
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Rep: Reputation: 17
Case-Sensitive char comparing in MySQL


Hello Geeks
How you doing?

I need to do a check to make sure an entered string matches the one stored into the db.
For example, a password field.


I know something to achieve that by:

Code:
SELECT something FROM sometbl WHERE string1 LIKE BINARY string2;
Alright, is that the best way, or is there better ways?
The table will be a huge table, that suppose to hold more than 5 millions record, and reading of that table will depend on sensitive string comparison, which means I need a very smart code that perform good.


Hope I explained well..
Thanks
 
Old 03-04-2010, 07:21 AM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
If you are designing the database then make sure you know what case the strings will be in by converting every string to upper or lower case before it gets stored.

Convert the input to lower case as you first insert it to the record.
Code:
INSERT INTO sometbl (field1,field2) VALUES(LOWER('string1'),LOWER('string2'));

Once you have records in the table you can then act accordingly.

If your input is string2, and the records are all in lower case then you can select like :
Code:
SELECT something FROM sometbl WHERE field2=LOWER('string2');
If your input is string2, and the records are all in upper case then you can select like :
Code:
SELECT something FROM sometbl WHERE field2=UPPER('string2');
If the database has already got mixed case entries, then you can still follow the same approach but if you are relying on the case to distinguish records you may get errors. Do you reject an entry because it has a different case ? Only you can tell.

Last edited by smoker; 03-04-2010 at 07:26 AM.
 
0 members found this post helpful.
Old 03-04-2010, 07:35 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You will want to index the fields that you are searching on. That way MySQl will be able to optimise your query.
 
1 members found this post helpful.
Old 03-04-2010, 07:38 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@smoker: I totally disagree with you on your approach!

The database should hold the data as is (not made fully upper/lower case). If needed(!!) you can let the frontend do the conversion to lower/upper and check this against the entered data.

Simple example:
myPassword, MYPassword and MYPASSWORD are three unique passwords, converting them to upper (or lower) case would increase the possibility of entering the wrong password and still get access.

If speed is a concern, look into indexing (as already mentioned by graemef....).
 
Old 03-04-2010, 08:12 AM   #5
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Quote:
Originally Posted by druuna View Post
Hi,

@smoker: I totally disagree with you on your approach!

The database should hold the data as is (not made fully upper/lower case). If needed(!!) you can let the frontend do the conversion to lower/upper and check this against the entered data.

Simple example:
myPassword, MYPassword and MYPASSWORD are three unique passwords, converting them to upper (or lower) case would increase the possibility of entering the wrong password and still get access.

If speed is a concern, look into indexing (as already mentioned by graemef....).
In that case all he has to do is use WHERE field1=string2

Did you read my post ?
Quote:
If the database has already got mixed case entries, then you can still follow the same approach but if you are relying on the case to distinguish records you may get errors. Do you reject an entry because it has a different case ? Only you can tell.
He asked about "Case-Sensitive char comparing"
He didn't specify a field in his select.
 
Old 03-04-2010, 08:14 AM   #6
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Case-sensitive string comparison is the default, so all you have to do is:

Code:
SELECT SomeColumns FROM SomeTable WHERE ColumnYoureSearching = 'ValueYoureSearchingFor';
 
Old 03-04-2010, 08:20 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@smoker: Yes I did read your post

The OP did mention a field, the password field to be specific......

But besides that, storing data that is converted to upper/lowercase into a DB is, in general, bad practice. It will get one into trouble in the long run (talking from experience I had with several companies).

Anyway, it's up to the OP to implement what s/he finds best.
 
Old 03-04-2010, 08:34 AM   #8
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
@druuna
My original point was related to not knowing what the input case was.
If someone searches the db for Hello but the db contains hello, then it won't get a match by default.
So you either have to search for all versions, which takes time, or you ensure that there is only one version possible.
I was just pointing out a way to do that using mysql.

In the case of passwords, I agree with you, but why was he trying to use BINARY ?
He actually said "for example a password field" but he didn't say that was the only case scenario.
 
Old 03-04-2010, 08:51 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by smoker View Post
@druuna
My original point was related to not knowing what the input case was.
If someone searches the db for Hello but the db contains hello, then it won't get a match by default.
So you either have to search for all versions, which takes time, or you ensure that there is only one version possible.
I was just pointing out a way to do that using mysql.
Yes, I understand you gave a possible solution, which is bad practice as I pointed out.

In your scenario (Hello in the DB and hello as searchstring) you use something like:

SELECT UPPER(SomeColumns) FROM SomeTable WHERE ColumnYoureSearching like UPPER("ValueYoureSearchingFor");

No need to check all possible variations.
 
Old 03-04-2010, 05:15 PM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
If your input is mixed case and you want to find all versions then use LIKE which is case insensitive:

Code:
SELECT somefiledlsit FROM mytable WHERE field1 LIKE filed2
 
Old 03-04-2010, 08:32 PM   #11
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Original Poster
Rep: Reputation: 17
Oh, great to see my topic went into a good discussion, which bring back great results.

Well, in fact..
I do really need to store data in both upper, and lower case..
That will give me a much wider range to store more unique strings with less letters..
And, that's the whole point.

The stored data into that filed, will be random generated string, upper,lower and numbers combined together..
Examples are:
  • Eida3Do
  • jjO298GJj
  • aNd-s0-0n

Thus, the idea of changing the case, either while comparing, or while storing will not help..!!


Any way, I'll try @graemef idea, to see.
and so, I need to make sure that I'll do it right..

So, is all I need in my case it to create an index on that mentioned field?


Cheers
 
Old 03-04-2010, 09:08 PM   #12
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by druuna View Post
Hi,
If speed is a concern, look into indexing (as already mentioned by graemef....).
Yeah, really it's..
Since, this website might be so much loaded..
and I need my code to be sharp and smooth.

Thanks
 
Old 03-04-2010, 10:45 PM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
At the end of the day it depends on how you defined the table/column in question.

By default yourSQL (I refuse to call it "my" :D) will choose case insensitive
storage.
Attached Thumbnails
Click image for larger version

Name:	case.jpg
Views:	14
Size:	54.2 KB
ID:	2897  
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache 2.x case-sensitive jarias Linux - Newbie 2 05-15-2009 05:11 PM
running mysql on case insensitive and sensitive on same server. jindalarpan Linux - Server 0 09-22-2008 04:57 AM
Copying files from case-sensitive Linux to case-insensitive Windows via CIFS? SlowCoder Linux - General 4 05-07-2008 07:03 PM
proftd + mysql = case sensitive auth? zooper Linux - Server 0 01-16-2007 11:51 PM
mySQL LIKE search is case sensitive? icepig Linux - Software 4 11-14-2003 04:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration