Summary of commonly used multi-table modification statements in Mysql and Oracle

Summary of commonly used multi-table modification statements in Mysql and Oracle

I saw this question in the SQL training question bank today. This is a very representative multi-table modification question. In fact, it is not difficult to solve this question. There are many solutions in both MySQL and Oracle. Next, I will summarize these solutions.

Multiple table modification in msyql

For the commonly used multi-table modification statements in MySQL, let's use examples to explain them.

//Create table create table aaa(id int,value1 int(5),value2 int(5),value3 int(5),value4 int(5));
create table bbb like aaa;
//Insert into aaa values(1,0,0,0,0),(2,0,0,0,0),(3,0,0,0,0),(4,0,0,0,0),(5,0,0,0,0),(6,0,0,0,0),(7,0,0,0,0);
insert into bbb values(1,11,12,13,14),(3,31,32,33,34),(5,51,52,53,54);
//Backup create table a1 select * from aaa;
create table b1 select * from bbb;

General multi-table modification: (applicable to both MySQL and Oracle, and Oracle will be explained below)

//General multi-table modification update a1 set a1.value1 = (select b1.value1 from b1 where a1.id = b1.id),
a1.value2 = (select b1.value2 from b1 where a1.id = b1.id);

Table association multi-table modification (this only applies to MySQL)

//Table association multi-table modification update a1 inner join b1 on a1.id = b1.id set a1.value1 = b1.value1,
a1.value2 = b1.value2,a1.value3 = b1.value3;

Multiple table modification in Oracle

--Create table create table aaa(id number,value1 number,value2 number,value3 number,value4 number);
create table bbb(id number,value1 number,value2 number,value3 number,value4 number);
--Insert datainsert into aaa values(1,0,0,0,0);insert into aaa values(2,0,0,0,0);insert into aaa values(3,0,0,0,0);
insert into aaa values(4,0,0,0,0);insert into aaa values(5,0,0,0,0);insert into aaa values(6,0,0,0,0);
insert into aaa values(7,0,0,0,0);insert into bbb values(1,11,12,13,14);
insert into bbb values(3,31,32,33,34);insert into bbb values(5,51,52,53,54);
-- Backup create table a1 as select * from aaa;
create table b1 as select * from bbb;

General multi-table modification:

Like the above MySQL statement, this way of writing is relatively easy to understand, but the efficiency is really unsatisfactory. If your multi-table modification only needs to modify one column (like the top question), this way of writing is fine.

//General multi-table modification update a1 set a1.value1 = (select b1.value1 from b1 where a1.id = b1.id),
a1.value2 = (select b1.value2 from b1 where a1.id = b1.id);

However, this is not very practical when it comes to multi-column modifications. Oracle provides a syntax that allows us to modify and add multiple tables.

Merge into

merge into is a combination of update and insert statements, which can realize the functions of update and insert at the same time. I will not explain it here, but briefly introduce the syntax of merge into.

merge into target table a ---The target table pointed to by into is the table we want to update.
using source table b ---The table pointed to by using cannot be changed. It can also be a view, result set, etc.
on(a.id = b.id) --- on() contains the conditions for matching the target table and the source table. If the conditions are met, the match is successful.
when matched then ---when matched then is followed by a modification statement.
	update set ... 
when not matched then ---when not matched then is followed by a new statement.
	insert values(....);
merge into a1 using b1 
on (a1.id = b1.id) 
when matched then update 
	set a1.value1 = b1.value1,a1.value2 = b1.value2,a1.value3 = b1.value3;

exists multi-table modification

EXISTS also applies to MySQL and Oracle. If there is no hard requirement to modify table A based on the data in table B, it is also acceptable to use EXISTS. The SQL execution order after using exists is to execute the outer query first and then the inner query, so the value of table a1 cannot be changed to the value of table a2 in multi-table modification.

update a1 set a1.value1 = 99,a1.value2 = 999,a1.value3 = 999 
where exists(select value1 from b1 where a1.id = b1.id);

This concludes this article about the commonly used multi-table modification statements in Mysql and Oracle. For more information about the commonly used multi-table modification statements in Mysql and Oracle, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Description of the default transaction isolation level of mysql and oracle
  • A brief discussion on the differences between the three major databases: Mysql, SqlServer, and Oracle
  • Detailed explanation of paging query methods in mysql, mssql and oracle
  • SQL to implement time series dislocation restoration case

<<:  Docker cleaning killer/Docker overlay file takes up too much disk space

>>:  TypeScript enumeration basics and examples

Recommend

Analysis of CocosCreator's new resource management system

Table of contents 1. Resources and Construction 1...

MySQL Query Cache and Buffer Pool

1. Caches - Query Cache The following figure is p...

Docker container from entry to obsession (recommended)

1. What is Docker? Everyone knows about virtual m...

What is JavaScript anti-shake and throttling

Table of contents 1. Function debounce 1. What is...

VMware vCenter 6.7 installation process (graphic tutorial)

background I originally wanted to download a 6.7 ...

What you need to know about filters in Vue

Table of contents Preface What is a filter How to...

Implementation of textarea adaptive height solution in Vue

Table of contents Hidden Problems Solution to ada...

MySQL trigger detailed explanation and simple example

MySQL trigger simple example grammar CREATE TRIGG...

Installation and daemon configuration of Redis on Windows and Linux

# Installation daemon configuration for Redis on ...

Mysql date formatting and complex date range query

Table of contents Preface Query usage scenario ca...

A friendly alternative to find in Linux (fd command)

The fd command provides a simple and straightforw...

4 ways to implement routing transition effects in Vue

Vue router transitions are a quick and easy way t...