Welcome, Sign in or Register

MySQL update with WHERE clause containing two indexed columns

POSTED IN Computers and Internet  WRITTEN BY: kabudu on 8 Dec, 2009 at 01:41 PM
Comments 1 COMMENT

Supposing you had a MySQL table that stored user details, for simplicity lets call the table 'users' and you had 2 indexes:

1) The auto_increment primary key (users_id)
2) An index on the username (username)

If you were to run an update on a row for example:

UPDATE users SET first_name='tom' WHERE users_id=10 AND username='tomtank';

Naturally you would expect this query to run and perform the update. However, thats not what I discovered. What actually happened was, MySQL ran the query but did not perform the update and reported zero rows affected. However, as soon as I removed the second condition in the WHERE clause, the results were as expected.

Of course one may argue for the necessity of the second condition in the WHERE clause, but I did that simply out of vanity. :)

This is a strange one indeed, and might though I'm not 100% sure be attributed to my version of MySQL (5.1.33). Whatever the case is, I just thought I would share this experience in the hope that it  may save someone alot of time and digging.



LAST UPDATED: 8 Dec, 2009 at 01:46 PM
 

Comments

Why will you want to satisfy both conditions anyway, since the ID primary key is unique and should have been indexed instead of the letters username. You still use 5.1.33! :)

by Kejau Touray on 6 Jan, 2010 at 01:50 PM

Add your comment:


[We won't publish this]

[Optional]
© 2008 Portaltoafrica.com. All rights reserved