当前位置:网站首页>Three methods of Oracle two table Association update
Three methods of Oracle two table Association update
2022-07-06 04:58:00 【Terence-Wang】
Not much said , Let's do the experiment .
preparation
Create the following table data
drop table demo_t1;
drop table demo_t2;
CREATE TABLE DEMO_T1
(
FNAME VARCHAR2(20)
, FMONEY VARCHAR2(20)
);
ALTER TABLE demo_t1 ADD PRIMARY KEY(FNAME);
insert into demo_t1 (fname,fmoney) values('A','20');
insert into demo_t1 (fname,fmoney) values('B','30');
CREATE TABLE DEMO_T2
(
FNAME VARCHAR2(20)
, FMONEY VARCHAR2(20)
);
ALTER TABLE demo_t2 ADD PRIMARY KEY(FNAME);
insert into demo_t2 (fname,fmoney) values('C','10');
insert into demo_t2 (fname,fmoney) values('D','20');
insert into demo_t2 (fname,fmoney) values('A','100');
Current demand : reference T2 surface , modify T1 surface , The modification condition is two tables fname Columns are consistent .
The way 1:update
UPDATE DEMO_T1 t1
SET T1.FMONEY = (select T2.FMONEY from DEMO_T2 T2 where T2.FNAME = T1.FNAME)
WHERE EXISTS(SELECT 1 FROM DEMO_T2 T2 WHERE T2.FNAME = T1.FNAME);
If you update multiple fields at the same time, you can refer to the following syntax :
UPDATE DEMO_T1 t1
SET ( Field one , Field 2 ,...) = (select Field one , Field 2 ,... from DEMO_T2 T2 where T2.FNAME = T1.FNAME)
WHERE EXISTS(SELECT 1 FROM DEMO_T2 T2 WHERE T2.FNAME = T1.FNAME);
The way 2: Inline view update
Be careful : Tables requiring data retrieval , This field must be a primary key or have a unique constraint
UPDATE (
select t1.fmoney fmoney1,t2.fmoney fmoney2 from demo_t1 t1,demo_t2 t2 where t1.fname = t2.fname
)t
set fmoney1 =fmoney2;
The way 3:merge to update
merge into demo_t1 t1
using (select t2.fname,t2.fmoney from demo_t2 t2) t
on (t.fname = t1.fname)
when matched then
update set t1.fmoney = t.fmoney;
边栏推荐
- Project manager, can you draw prototypes? Does the project manager need to do product design?
- web工程导入了mysql驱动jar包却无法加载到驱动的问题
- 2021 RoboCom 世界机器人开发者大赛-本科组(复赛)
- RTP gb28181 document testing tool
- [detailed steps of FreeRTOS shift value for the first time]
- Codeforces Round #804 (Div. 2)
- 驱动开发——第一个HelloDDK
- Summary of redis AOF and RDB knowledge points
- Supreme Court, judgment standard of divorce cases
- Microblogging hot search stock selection strategy
猜你喜欢
Zynq learning notes (3) - partial reconfiguration
Introduction of several RS485 isolated communication schemes
Vulnerability discovery - vulnerability probe type utilization and repair of web applications
你需要知道的 TCP 三次握手
Simple understanding of interpreters and compilers
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Sqlserver query results are not displayed in tabular form. How to modify them
DMA use of stm32
SQL injection vulnerability (MSSQL injection)
几种RS485隔离通讯的方案介绍
随机推荐
The web project imported the MySQL driver jar package but failed to load it into the driver
[lgr-109] Luogu may race II & windy round 6
最高法院,离婚案件判决标准
Pagoda configuration mongodb
Uva1592 Database
[Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
ISP learning (2)
驱动开发——HelloWDM驱动
Platformio create libopencm3 + FreeRTOS project
集合详解之 Map + 面试题
IPv6 comprehensive experiment
[noip2008 improvement group] stupid monkey
Sorting out the knowledge points of multicast and broadcasting
ORM aggregate query and native database operation
组播和广播的知识点梳理
内核判断i2c地址上是否挂载外设
Summary of three log knowledge points of MySQL
[noip2009 popularization group] score line delimitation
Microblogging hot search stock selection strategy
也算是学习中的小总结