Summary: Updating existing data is one of the most important task when you work with database. In this tutorial, you will learn how to use MySQL UPDATE statement to update data in database tables.

SQL UPDATE statement is used to update existing data in database tables. It can be used to change values of single row, group of rows or even all rows in a table. In MySQL, the SQL UPDATE statement form is as below:

1 UPDATE [LOW_ PRIORITY] [IGNORE] table_name [, table_name...]
2 SET column_name1=expr1
3 [, column_name2=expr2 ...]
4 [WHERE condition]

Let’s examine the MySQL UPDATE statement in details:

  • Followed by the UPDATE keyword is the name of a table you want to change the data. In MySQL, you can change the data of multiple tables at a time. If an UPDATE command violates any integrity constraint, MySQL does not perform the update and it will issue an error message.
  • The SET clause determines the columns’ name and the changed values. The changed values could be a constant value, expression or even a subquery.
  • WHERE clause determines which rows of the tables will be updated. It is an optional part of SQL UPDATE statement. If WHERE clause is ignored, all rows in the tables will be updated. The WHERE clause is so important that you should not forget. Sometimes you want to change just one row of a table but you forget WHERE clause so you accidentally update the whole table.
  • LOW_ PRIORITY keyword is used to delay the execution until no other client applications reading data from the table. This is used for controlling the update process in MySQL database server.
  • IGNORE keyword is used to execute the update even errors occurred during the execution. The errors in the update process could be duplicate value on unique column, or new data does not match with the column data type. In the first situation data is not updated and in the second one MySQL tries to convert the data into closest valid values.

Let’s practice with a couple of examples in our sample database to understand more about SQL UPDATE statement.

In employees table, if you want to update the email of Mary Patterson with employeeNumber 1 with the new email as mary-patterso@sdfsdf.com, you can execute the following query:

Make sure we are truly updating the data we select it first to see what the current data looks like.

1 SELECT firstname,
2 lastname,
3 email
4 FROM employees
5 WHERE employeeNumber = 1
+-----------+-----------+--------------------------------+
| lastname  | firstname | email                          |
+-----------+-----------+--------------------------------+
| Patterson | Mary      | mpatterso@sdfsdf.com |
+-----------+-----------+--------------------------------+
1 row in set (0.02 sec)

Update her email to the new email mary-patterso@sdfsdf.com

1 UPDATE employees
2 SET email = 'mary-patterso@classicmodelcars.com'
3 WHERE employeeNumber = 1

Execute the select query above again; you will see the email change to the new value.

+-----------+-----------+------------------------------------+
| lastname  | firstname | email                              |
+-----------+-----------+------------------------------------+
| Patterson | Mary      | mary-patterso@sdfsdf.com |
+-----------+-----------+------------------------------------+
1 row in set (0.00 sec)

In this tutorial, you’ve learned how to use SQL UPDATE statement in MySQL to update data of database tables.

Related posts:

  1. Inserting Data into Database Tables
  2. Using MySQL SELECT Statement to Query Data
  3. Working with Tables – Part II
  4. How to Use MySQL Limit to Constrain Number of Returned Records
  5. Retrieving Data in a Range Using SQL BETWEEN

Leave your comment

You must be logged in to post a comment.


Link Exchange Spritual Place for Bhakti | bhaktiguru.com Business Franchise in India | businessfranchiseindia.com Free Directory Submission & Link Submission | ab-directory.com Big Dwarka Business Listing | bigdwarka.com Kolkata Online Business Listing | onlinekolkata.com Patna Online Business Listing | onlinepatna.com Dwarka Online Business Listing | onlinedwarka.com Bihar Online | First Bihar's Portal | bihar-online.com Big Bihar Business Listing | bigbihar.com Dynamic Website Development | dynamicwebsitedeveloper.com Noida Online Business Listing | onlinenoida.com SEO Service Delhi & India | seoservicedelhi.com SEO Experts Delhi & India | seoexpertsdelhi.com Big Patna Business Listing | bigpatna.com Bihar schools Listing Free Schools Ad | biharschools.com Creative Websoft Solutions | Web Development India | creativewebsoft.us Bulk SMS Providers India | bulksmsproviders.net Online cbse Guides | onlinecbseguides.com Big Kolkata Business Listing | bigkolkata.com Handicap Shaadi.com | handicapshaadi.com Handicap Marriage.com | handicapmarriage.com India Home Tutors.com | indiahometutors.com Indian Property Guides.com | indianpropertyguides.com Online Stocks Community.com | onlinestockscommunity.com Free Article Submission | ab-articles.com Secret Dating Online.com | secretdatingonline.com Private DatingOnline.com | privatedatingonline.com Indian businessguides.com | indianbusinessguides.com Software Engineer Delhi | Meenu Khanna Bollywood & Hot Images | hibdy.com Free Computer & Online PHP Training | php2php.com Free Ignou Project & Assignment | ignouinfo.com PHP Programmer India | Web Developer India | bageshsingh.com Creative Websoft Solutions | Web Development IndiaBlog Link Exchange http://www.php2php.com/tutorial-blog/http://bageshsingh.com/bagesh-blog/http://www.bhaktiguru.com/bhaktigurublog/http://www.hibdy.com/desi-masala-blog/