Purpose
This statement is used to modify the values of existing fields 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 the hint options. |
| dml_table_clause | Specifies the name of the table, view, or column returned by a special subquery to be updated. Note If you specify view, the database updates the base table of the view. However, you cannot update multiple base tables through a view. |
| where_condition | Specifies the filter conditions. |
| update_asgn_list | Specifies the update list. |
| returning_exprs | Specifies the projected columns after the data is modified. |
| into_clause | Inserts the projected columns after the data is modified into the specified list. |
| into_var_list | Inserts the specified projected columns into the specified variable list. |
| ref_name | The name of the variable. |
Notice
A special subquery refers to a subquery similar to the one 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 the tbl1 table 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
Update a single table: Modify the
col2column value of the row in thetbl1table wheretbl1.col1=1to100.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 setUpdate a single table: Modify the
col2column value of the row in the subquery wherev.col1=1to10.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 setUpdate a single table: Modify the
col2column value of the rows in thetbl1table wheretbl1.col1<3to100.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 setUpdate a single table: Use the
RETURNINGclause to return 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 the sample table.
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);Perform a table update operation using variables.
-- Declare variables DECLARE var_emp employees1%ROWTYPE; BEGIN -- Get data from employees1 and store it in 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; /