I am a true fan of mySQL and one of my favorite statements is the UPDATE statement. One of the big needs for any website with a wiki or with user generated content is the ability to edit content drawn from a database. The UPDATE statement is the statement that allows a webmaster to do this.
In the majority of uses it is composed of 3 main parts (but of course can do a lot more):
UPDATE tablename SET column='value' WHERE column='value'
The example I will user here is a user table which for the sake of argument I shall call tbl_user and will have four simple columns. One with their username (A_STR_username), one with their password (A_STR_password), one with their number of logins (A_INT_usertotallogins) and finally one with their user description (A_BLOB_userdescription)
A_STR_username | A_STR_password | A_INT_usertotallogins | A_BLOB_userdescription |
George | r3db0y | 2 | UPDATE SET SELECT INTO WHERE AND SET UPDATE WHERE UPDATE SELECT INTO AND |
Patrick | fr33d0m | 27 | UPDATE SET SELECT INTO WHERE AND SET UPDATE WHERE UPDATE SELECT INTO AND |
Jimmy | cr3l4t | 4 | UPDATE SET SELECT INTO WHERE AND SET UPDATE WHERE UPDATE SELECT INTO AND |
Jane | l4gun4 | 15 | UPDATE SET SELECT INTO WHERE AND SET UPDATE WHERE UPDATE SELECT INTO AND |
Now say I want to update George's user description that can be done really easily using the UPDATE command in a mySQL database. I would want to ensure that I only upate George if he has given me the right password though so...
UPDATE tbl_users SET A_BLOB_userdescription = 'I love mySQL UPDATE statements!' WHERE A_STR_username='George' AND A_STR_password = 'r3db0y'
This query will only update a row where the username is George and the password is redboy and otherwise will throw and error. This is awesome in my opinion.
A_STR_username | A_STR_password | A_INT_usertotallogins | A_BLOB_userdescription |
George | r3db0y | 2 | I love mySQL UPDATE statements! |
Patrick | fr33d0m | 27 | UPDATE SET SELECT INTO WHERE AND SET UPDATE WHERE UPDATE SELECT INTO AND |
Jimmy | cr3l4t | 4 | UPDATE SET SELECT INTO WHERE AND SET UPDATE WHERE UPDATE SELECT INTO AND |
Jane | l4gun4 | 15 | UPDATE SET SELECT INTO WHERE AND SET UPDATE WHERE UPDATE SELECT INTO AND |
There is one more little trick I want to share which I enjoy with the mySQL UPDATE statement and that is the ability to perform math on a column. I will do a simple addition but there is loads more you can do. A use case for this example could be incrementing the number of time Jimmy has logged into your site
UPDATE tbl_users SET A_INT_usertotallogins = A_INT_usertotallogins+1 WHERE A_STR_username='Jimmy' AND A_STR_password = 'cr3l4t'
The result of this change will be just to increment the log total logins column of the table (as you see below). Really beautifully simple to do and yet really useful in a whole host of cases... I hope you learn to love the mySQL UPDATE statement too (and of course it works in more than just mySQL).
A_STR_username | A_STR_password | A_INT_usertotallogins | A_BLOB_userdescription |
George | r3db0y | 2 | I love mySQL UPDATE statements! |
Patrick | fr33d0m | 27 | UPDATE SET SELECT INTO WHERE AND SET UPDATE WHERE UPDATE SELECT INTO AND |
Jimmy | cr3l4t | 5 | UPDATE SET SELECT INTO WHERE AND SET UPDATE WHERE UPDATE SELECT INTO AND |
Jane | l4gun4 | 15 | UPDATE SET SELECT INTO WHERE AND SET UPDATE WHERE UPDATE SELECT INTO AND |