English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use SQL to update records in the database table.
In the previous chapters, we learned how to update table data based on various conditionsInsert dataand how to insert data from the database tableSelect query data. In this tutorial, we will learn to perform a more important task, that is, to update existing records in the database table.
The UPDATE statement is used to update existing data in the table.
UPDATE table_name SET column1_name = value1, column2_name = value2,...WHERE condition;
Here,column1_name,column2_name,...is the name of the database table column or field to be updated. You can also use the OR and AND operators learned in the previous chapterCombine multiple conditions.
Warning:The WHERE clause in the UPDATE statement specifies the records to be updated. If the WHERE clause is omitted, all records will be updated.
Let's look at some examples to demonstrate how it actually works.
Suppose we have athe employees tabletable, which has the 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 | +--------+--------------+------------+--------+---------+
The following SQL statement will updatethe employees tabletableemp_namea field and set a new value, where employee id (i.e.emp_id)equals3.
UPDATE employees SET emp_name = 'Sarah Ann Connor' WHERE emp_id = 3;
After execution, the result table will be as follows:
+--------+------------------+------------+--------+---------+ | 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 | +--------+------------------+------------+--------+---------+
Similarly, you can use a list of column names and value pairs separated by commas to update multiple columns. The following example will updatethe employees tableinemp_idto5 of the existing employeessalaryanddept_idField.
UPDATE employees SET salary = 6000, dept_id = 2 WHERE emp_id = 5;
After execution, the result table will be as follows:
+--------+------------------+------------+--------+---------+ | 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 | +--------+------------------+------------+--------+---------+