This Oracle tutorial explains how to use the Oracle UPDATE statement with syntax, examples, and practice exercises.
The Oracle UPDATE statement is used to update existing records in a table in an Oracle database. There are 2 syntaxes for an update query in Oracle depending on whether you are performing a traditional update or updating one table with data from another table.
The syntax for the UPDATE statement when updating one table in Oracle/PLSQL is:
OR
The syntax for the Oracle UPDATE statement when updating one table with data from another table is:
The columns that you wish to update.
The new values to assign to the column1, column2, ... column_n. So column1 would be assigned the value of expression1, column2 would be assigned the value of expression2, and so on.
Optional. The conditions that must be met for the update to execute. If no conditions are provided, then all records in the table will be updated.
Let's look at a very simple Oracle UPDATE query example.
This Oracle UPDATE example would update the last_name to 'James' in the customers table where the customer_id is 5000.
Let's look at an Oracle UPDATE example where you might want to update more than one column with a single UPDATE statement.
When you wish to update multiple columns, you can do this by separating the column/value pairs with commas.
This Oracle UPDATE statement example would update the state to 'California' and the customer_rep to 32 where the customer_id is greater than 100.
Let's look at an Oracle UPDATE example that shows how to update a table with data from another table.
This UPDATE example would update only the customers table for all records where the customer_id is less than 1000. When the supplier_name from the suppliers table matches the customer_name from the customers table, the contract_date from the suppliers table would be copied to the c_details field in the customers table.
You can also perform more complicated updates in Oracle.
You may wish to update records in one table based on values in another table. Since you can't list more than one table in the Oracle UPDATE statement, you can use the Oracle EXISTS clause.
For example:
In this Oracle UPDATE example, whenever a supplier_id matched a customer_id value, the supplier_name would be overwritten to the customer_name from the customers table.
Based on the suppliers table populated with the following data, update the city to "San Francisco" for all records whose supplier_name is "CISCO".
The following UPDATE statement would perform this update in Oracle.
The suppliers table would now look like this:
SUPPLIER_ID | SUPPLIER_NAME | CITY |
---|---|---|
5001 | GCP | Chicago |
5002 | CISCO | San Francisco |
5003 | AWS | Detroit |
5004 | MICRO | New York |
Based on the suppliers and customers table populated with the following data, update the city in the suppliers table with the city in the customers table when the supplier_name in the suppliers table matches the customer_name in the customers table.
The following UPDATE statement would perform this update in Oracle.
The suppliers table would now look like this:
SUPPLIER_ID | SUPPLIER_NAME | CITY |
---|---|---|
5001 | GCP | San Francisco |
5002 | CISCO | Toronto |
5003 | AWS | Newark |
5004 | MICRO | LA |