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 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 together 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 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 noticed that it must be stated at least a single table after the clause UPDATE. The data provided in the table is not defined after the clause UPDATE then 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 be assigning new column values in the G1 or/and G2 tables that 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 table. Firstly, let us view the syntax for Oracle update with 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 update 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:
- Col1, Col2, ….., ColN denotes the columns which is needed to be updated.
- Expr1, Expr2,….ExprN denotes the new values that is to be assigned to the respective columns such as Col1, Col2, ….., ColN type of arrangements.
- WHERE clause is an optional condition to be applied which meets for the update to execute. But if no conditions are provided then, all table row records will be updated.
How Update with join works 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 like 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 is 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 updates the table columns on the left side of the LEFT JOIN clause according to the matched values otherwise with NULL.
Examples
Let us illustrate few instances for Oracle Update with Join query as follows:
To update 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 just simply modify the column value of status to ‘In Process’ in the Orders table where the OrderNum is equal to 112.
To update more than one table columns:
Let us query the update statement command in oracle where multiple columns update can be performed with one update command query 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 be as shown above where mentioned column value pairs are updated.
To update with data from other table:
We will execute the update statement in Oracle that will update the columns values from other 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 execution of the cross-table update. We have also discussed the syntaxes with INNER and LEFT clauses using the UPDATE statement. In this way, the existing data in the database can be modified with this Oracle update with join query commands that helps to update within a same table or 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 –
14 Online Courses | 8 Hands-on Projects | 120+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses