Web Info and IT Lessons Blog...

Thursday 6 November 2014

MySql Update Query

The previous Lesson of MySql Lessons Series was about MySql Select Query and in this lesson we will learn about MySql Update Query.

MySql Update Query

MySql Update Query can be used to update one or more fields of one or more records in the table. For elaboration purpose, consider the same table we have been using in the previous lessons i.e

Table Data

Let us say we want to empty the email field of all the users in the above table. We can do it by updating the email column of all records with an empty value. This is how we can write the update query for it:


UPDATE `testtable` SET `email` = ''

The above query will empty the email column of all the users in the testtable. Now what if we want to update the email of only keith with some other value, then this is how we can do it:


UPDATE `testtable` SET `email` = 'keith@hotmail.com' WHERE 
`Name` = 'keith' 

The above query will update the email of user keith only because of the WHERE clause in the query.

Update Multiple Columns:


We can update more than one column of a user or a group of users using MySql Update Query. For example, let us update the Name and Email of the user Tom and set the Name to Johnson and Email to Johnson@gmail.com. This is how we can achieve this:


UPDATE `testtable` SET `Name` = 'Johnson',
`email` = 'Johnson@gmail.com' WHERE `Name` = 'Tom' 



No comments:

Post a Comment