SQL BASIC
SQL JOINS
SQL ADVANCED
SQL REFERENCE

SQL UPDATE Statement

In this tutorial you will learn how to update records in a database table using SQL.

Updating Table Data

In the previous chapters we've learnt how to insert data as well as how to select data from a database table based on various conditions. In this tutorial we'll learn to perform one more important task which is updating the existing records in a database table.

Syntax

The UPDATE statement is used to update existing data in a table.

UPDATE table_name
SET column1_name = value1, column2_name = value2,...
WHERE condition;

Here, column1_name, column2_name,... are the names of the columns or fields of a database table whose values you want to update. You can also combine multiple conditions using the AND or OR operators, that you've learned in the previous chapters.

Warning: The WHERE clause in the UPDATE statement specifies which record or records should be updated. If you omit the WHERE clause, all the records will be updated.

Let's check out some examples that demonstrate how it actually works.

Suppose we've an employees table in our database that has following records:

+--------+--------------+------------+--------+---------+
| emp_id | emp_name     | hire_date  | salary | dept_id |
+--------+--------------+------------+--------+---------+
|      1 | Ethan Hunt   | 2001-05-01 |   5000 |       1 |
|      2 | Tony Montana | 2002-07-15 |   6500 |       5 |
|      3 | Sarah Connor | 2005-10-18 |   8000 |       3 |
|      4 | Rick Deckard | 2007-01-03 |   7200 |       4 |
|      5 | Martin Blank | 2008-06-24 |   5600 |    NULL |
+--------+--------------+------------+--------+---------+

Updating a Single Column

The following SQL statement will update the emp_name field of the employees table and set a new value where the employee id i.e. emp_id is equal to 3.

UPDATE employees SET emp_name = 'Sarah Ann Connor'
WHERE emp_id = 3;

After execution, the resulting table will look something like this:

+--------+------------------+------------+--------+---------+
| emp_id | emp_name         | hire_date  | salary | dept_id |
+--------+------------------+------------+--------+---------+
|      1 | Ethan Hunt       | 2001-05-01 |   5000 |       1 |
|      2 | Tony Montana     | 2002-07-15 |   6500 |       5 |
|      3 | Sarah Ann Connor | 2005-10-18 |   8000 |       3 |
|      4 | Rick Deckard     | 2007-01-03 |   7200 |       4 |
|      5 | Martin Blank     | 2008-06-24 |   5600 |    NULL |
+--------+------------------+------------+--------+---------+

Updating Multiple Columns

Similarly, you can update multiple columns using a list of comma-separated column name and value pair. The following example will update the salary and dept_id field of an existing employee in the employees table whose emp_id is 5.

UPDATE employees
SET salary = 6000, dept_id = 2
WHERE emp_id = 5;

After execution, the resulting table will look something like this:

+--------+------------------+------------+--------+---------+
| emp_id | emp_name         | hire_date  | salary | dept_id |
+--------+------------------+------------+--------+---------+
|      1 | Ethan Hunt       | 2001-05-01 |   5000 |       1 |
|      2 | Tony Montana     | 2002-07-15 |   6500 |       5 |
|      3 | Sarah Ann Connor | 2005-10-18 |   8000 |       3 |
|      4 | Rick Deckard     | 2007-01-03 |   7200 |       4 |
|      5 | Martin Blank     | 2008-06-24 |   6000 |       2 |
+--------+------------------+------------+--------+---------+
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties