Updated May 20, 2023
Definition of Oracle Update with Join
Oracle Update with Join is a query command which is responsible for performing the cross-table update. Basically, with this update query statement, we will implement the INNER JOIN and LEFT JOIN clauses. In a database management system, we regularly apply the JOINS clauses to query specific row records from a table which can include (in the INNER JOIN query case) or may not include (in the LEFT JOIN query case) the commonly matched rows in another table. Hence, these JOIN clauses are operated with the update statement to function the query for the cross-table update. This Oracle Update with Join command defines to implement query which executes to update the present table or database records in the Oracle server.
Syntax:
Simply writing about the syntax structure, we have the following command for the update query with JOIN clauses:
UPDATE G1, G2, {INNER JOIN | LEFT JOIN} G1 ON G1.C1 = G2.C2 SET G1.C2 = G2.C2,
G2.C3 = expre WHERE conditional expressional;
Let us explain in detail the above-used syntax:
- Initially, we will define the main table as G1 and the table which we require the main table to join to as G2 after the clause UPDATE. It should be noted that it must be stated at least a single table after the clause UPDATE. If the data provided in the table is not defined after the clause UPDATE, it will not be modified.
- After this, state a type of JOIN clause you need to use, which can be either the LEFT or INNER JOIN clauses with a JOIN predicate but the JOIN clause should be displayed right after the UPDATE clause statement.
- Next, we will assign new column values in the G1 or/and G2 tables you need to update.
- Further, we will identify a condition using the WHERE clause for limiting table rows to rows to update.
Mainly, there are a couple of syntaxes to perform a query update in Oracle, either by depending on a traditional update or modifying one table with data records from another. Firstly, let us view the syntax for Oracle update with the Join clause to update a single table typed as follows:
UPDATE table SET Col1 = exp1, Col2 = exp2, ……, ColN = ExpN {WHERE conditional expressions};
OR,
The other type of Oracle syntax for updating with join while updating a single table with data from the other table is as follows:
UPDATE tableA SET Col1 = (SELECT expr1 FROM tableB WHERE conditional expressional)
{conditional expressional};
Let us discuss the parameters or arguments as follows:
- Col1, Col2, ….., ColN denotes the columns which are needed to be updated.
- Expr1, Expr2,….ExprN denotes the new values assigned to the respective columns such as Col1, Col2, ….., ColN type of arrangements.
- The WHERE clause provides an optional condition for the update to execute. However, if no conditions are specified, the update will apply to all table row records.
How update with join work in Oracle?
The Update with Join query works in Oracle by the following steps where we have supposed to have two tables, Orders and OrderInfo, shown below, which will be simple to elaborate also:
SELECT * FROM Orders;
SELECT * FROM OrderInfo;
Now, we will perform the update query with JOIN clause on table orderInfo based on the Orders table column values so that the resultant table provides an output of join using the query command as:
Update orderinfo INNER JOIN Orders ON orderinfo.ordernum = orders.OrderNum SET orderinfo.orderlinenum = orders.customernum;
SELECT * FROM OrderInfo;
The output will be as an updated orderinfo table with updated new column values as follows:
As you can see in the above output table that the orderinfo table’s column named orderlinenum values are replaced or updated with the column value present on the Order table’s custom enum.
In the same way, we can use the left join clause with the update statement so that the query results or update the table columns on the left side of the LEFT JOIN clause according to the matched values, otherwise with NULL.
Examples
Let us illustrate a few instances for Oracle Update with Join query as follows:
To update a single table column: We will execute a simple update query statement in Oracle written below and also view the table named Orders first to see the column values present as follows:
SELECT * FROM Orders;
Output:
Now, let us view the update query and its result after query execution as follows:
UPDATE Orders SET status = ‘In Process’ WHERE OrderNum = 112;
Output:
This Oracle Update query will modify the status column value to ‘In Process’ in the Orders table where the OrderNum equals 112.
To update more than one table column:
Let’s execute the update statement command in Oracle, which allows us to update multiple columns using a single query command as follows:
UPDATE Orders SET OrderDate = ‘2020-12-10’ , customernum = 180 WHERE OrderNum = 113;
SELECT * FROM Orders;
Output:
Here, to update multiple table columns, we have separated the value pairs with commas; therefore, the output will appear as above, updating the specified column-value pairs.
To update with data from another table:
We will execute the update statement in Oracle that will update the columns values from another table as follows:
For this, the other table is named as OrderInfo, and the table contents are as follows:
SELECT * FROM Orderinfo;
After that, the update query will be:
UPDATE OrderInfo SET Orderlinenum = (SELECT OrderNum FROM Orders WHERE Orders.OrderNum = OrderInfo.OrderNum) WHERE infoid>1;
SELECT * FROM Orderinfo;
If there are any updates, then the value of the table columns will be changed as:
Conclusion
In this article, we have learned about the Oracle Update with Join query, which is responsible for the execution of the cross-table update. Using the UPDATE statement, we have also discussed the syntaxes with INNER and LEFT clauses. This Oracle update with join query commands allows you to modify existing data in the database within the same table or across multiple tables.
Recommended Articles
This is a guide to Oracle Update with Join. Here we discuss the definition, syntax, and How update with join works in Oracle? With code implementation. You may also have a look at the following articles to learn more –