Purpose
This statement is used to modify the existing field values in a table.
Syntax
UPDATE [hint_options] dml_table_clause
SET update_asgn_list
[ WHERE where_condition]
[{ RETURNING | RETURN } returning_exprs [into_clause]]
dml_table_clause:
dml_table_name opt_table_alias
update_asgn_list:
column_name = expr [, expr...]
where_condition:
expression
returning_exprs:
projection_col_name [,projection_col_name ...]
into_clause:
{ INTO into_var_list | BULK COLLECT INTO into_var_list}
into_var_list:
{ USER_VARIABLE | ref_name } [,{ USER_VARIABLE | ref_name } ...]
Parameters
| Parameter | Description |
|---|---|
| hint_options | Specifies hint options. |
| dml_table_clause | Specifies the name of the table, view, or columns returned by a special subquery that you want to update. Notice If you specify view, the database updates the base table of the view. However, you cannot update multiple base tables through a single view. |
| where_condition | Specifies the filtering condition. |
| update_asgn_list | Specifies the update list. |
| returning_exprs | Specifies the projected columns after the data modification. |
| into_clause | Inserts the modified projected columns into the specified list. |
| into_var_list | Inserts the specified projected columns into the specified variable list. |
| ref_name | The variable name. |
Notice
A special subquery refers to a subquery similar to that corresponding to an updatable view. This type of subquery should not contain complex operators (such as GROUP BY, DISTINCT, and WINDOW FUNCTION).
Example 1
Create sample table tbl1 and insert test data.
obclient> CREATE TABLE tbl1(col1 INT PRIMARY KEY,col2 INT);
Query OK, 0 rows affected
obclient> INSERT INTO tbl1 VALUES(1,1),(2,2),(3,3),(4,4),(5,5);
Query OK, 5 rows affected
Records: 5 Duplicates: 0 Warnings: 0
obclient> SELECT * FROM tbl1;
+------+------+
| COL1 | COL2 |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+------+------+
5 rows in set
Single-table update: Change the
col2value to100for the row in tabletbl1wheretbl1.col1=1.obclient> UPDATE tbl1 SET tbl1.col2 = 100 WHERE tbl1.col1 = 1; Query OK, 1 row affected Rows matched: 1 Changed: 1 Warnings: 0 obclient> SELECT * FROM tbl1; +------+------+ | COL1 | COL2 | +------+------+ | 1 | 100 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | +------+------+ 5 rows in setSingle-table update: Directly operate the subquery. Change the
col2value to10for the row in the subquery wherev.col1=1.obclient> UPDATE (SELECT * FROM tbl1) v SET v.col2 = 10 WHERE v.col1 = 1; Query OK, 1 row affected Rows matched: 1 Changed: 1 Warnings: 0 obclient> SELECT * FROM tbl1; +------+------+ | COL1 | COL2 | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | +------+------+ 5 rows in setSingle-table update: Change the
col2value to100for the rows in tabletbl1wheretbl1.col1<3.obclient> UPDATE tbl1 SET tbl1.col2 = 100 WHERE tbl1.col1 < 3; Query OK, 2 rows affected Rows matched: 2 Changed: 2 Warnings: 0 obclient> SELECT * FROM tbl1; +------+------+ | COL1 | COL2 | +------+------+ | 1 | 100 | | 2 | 100 | | 3 | 3 | | 4 | 4 | | 5 | 5 | +------+------+ 5 rows in setSingle-table update: The
RETURNINGclause returns the modified data.obclient> UPDATE tbl1 SET tbl1.col2 = 1000 WHERE tbl1.col1 = 1 RETURNING col2; +------+ | COL2 | +------+ | 1000 | +------+ 1 row in set obclient> SELECT * FROM tbl1; +------+------+ | COL1 | COL2 | +------+------+ | 1 | 1000 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | +------+------+ 5 rows in set
Example 2
Create sample tables
CREATE TABLE employees1 ( id NUMBER, name VARCHAR2(20), salary NUMBER );CREATE TABLE employees2 ( id NUMBER, name VARCHAR2(20), salary NUMBER );Insert test data
INSERT INTO employees1 VALUES (1, 'Jack', 5000); INSERT INTO employees2 VALUES (1, 'Tom', 3000);Use a variable to perform the table update operation.
-- Declare a variable. DECLARE var_emp employees1%ROWTYPE; BEGIN -- Get data from the employees1 table to the variable. SELECT * INTO var_emp FROM employees1 WHERE id = 1; -- Use the variable to update the employees2 table. UPDATE employees2 SET (name) = (var_emp.name); COMMIT; END; /
