Detailed usage of Oracle Update Select

Detailed usage of Oracle Update Select

Oracle database is currently one of the most popular relational databases in the world, which has powerful data processing capabilities and rich functions. The update select command in Oracle SQL can update the data in one table to another table, and at the same time can realize data filtering and conversion, which is very practical.

There is a need in recent work. Now some data in the target table is basically the same as the source table, so you only need to move the data in the source table to the target table, and modify the data in different fields at the same time. When modifying a field, it is found that a condition needs to be specified, such as the primary key id, to modify a certain record. Such a modification is too inefficient. Is there a way to do batch operations?
This way is to use Oracle Update Select. This article will elaborate on the usage of Oracle Update Select from various aspects.

1. Basic usage
Oracle Update Select is often used to update the data in one table to another table. Its basic usage is as follows:

UPDATE table_name1 SET column_name1 = (SELECT column_name2 FROM table_name2 WHERE condition) WHERE condition; 

Among them, table_name1 is the table to be updated, column_name1 is the column to be updated, column_name2 is the column name in the data source table, table_name2 is the table name of the data source, and condition is the filter condition.

Here is a concrete example:

UPDATE employees SET salary = (SELECT salary * 1.1 FROM employees WHERE department_id = 80) WHERE department_id = 80; 

This statement increases the salary of employees in department number 80 by 10%.

Two, multi-table update
In addition to basic usage, Oracle Update Select can also implement multi-table update. For example: We have an order table orders and an order details table order_details, we want to update the total amount in the order table with the information in the order details table. The implementation method is as follows:

UPDATE orders o SET total_amount = ( SELECT SUM(unit_price * quantity) FROM order_details od WHERE od.order_id = o.order_id ) WHERE EXISTS ( SELECT 1 FROM order_details od WHERE od.order_id = o.order_id ); 

In this statement, we use EXISTS to determine whether the order exists in the order details table, and if so, update the total amount of the order.

3. Using functions and operators
With the help of functions and operators, we can implement richer update operations. For example: we have an order table orders, where the order_date column stores date-type data, and we want to convert them into year-month-day format. The implementation method is as follows:

UPDATE orders SET order_date = TO_CHAR(order_date, 'YYYY-MM-DD') WHERE order_date IS NOT NULL; 

In this statement, we use Oracle's TO_CHAR function to format the date data into a string and update it to the original data. Similarly, we can also use other functions and operators for data transformation and updating.

Fourth, use subqueries and join tables
Oracle Update Select can also use subqueries and join tables for update operations. For example: we have an order details table order_details, in which the product_id column stores the product ID, and the product name needs to be generated by connecting the table products, and the implementation method is as follows:

UPDATE order_details od SET product_name = ( SELECT product_name FROM products p WHERE p.product_id = od.product_id ) WHERE EXISTS ( SELECT 1 FROM products p WHERE p.product_id = od.product_id ); 

In this example, we connect the two tables orders_details and products, and use EXISTS to determine whether the product ID exists in the product table, and update the product name in the order details table if it exists.

V. Summary
Oracle Update Select is a very practical command that can greatly improve the efficiency and accuracy of data updates. There are three ways

  1. UPDATE tableName SET (a,b,c)=(SELECT a,b,c FROM ida WHERE ida.id=tableName.id);

  2. UPDATE tableName t1 SET a=(SELECT t2.a FROM ida t2 WHERE t1.id=t2.id),b=(SELECT t2.b FROM ida t2 WHERE t1.id=t2.id),c=(SELECT t2.c FROM ida t2 WHERE t1.id=t2.id)

  3. Each piece of data is executed as:UPDATE tableName SET (A,B,C)=(SELECT A,B,C FROM tableName WHERE id=''xxxxxx) WHERE id='xxxxxxx'

Through the introduction of this article, we can learn how to update single tables, multi-tables, use functions and operators, and connect tables in various situations. In actual database applications, we can choose appropriate methods to update data according to different needs and improve work efficiency.

This is the end of today's sharing

Welcome to like and comment

insert image description here

Guess you like

Origin blog.csdn.net/nings666/article/details/130967906