当前位置:网站首页>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;
边栏推荐
- Supreme Court, judgment standard of divorce cases
- 图论的扩展
- Redis 排查大 key 的4种方法,优化必备
- [classic example] binary tree recursive structure classic topic collection @ binary tree
- MySQL if and ifnull use
- Flody的应用
- Microblogging hot search stock selection strategy
- Rce code and Command Execution Vulnerability
- Principle and performance analysis of lepton lossless compression
- 也算是学习中的小总结
猜你喜欢
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Pagoda configuration mongodb
Postman assertion
Vulnerability discovery - vulnerability probe type utilization and repair of web applications
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Acwing week 58
Postman断言
Zynq learning notes (3) - partial reconfiguration
Extension of graph theory
SQL injection vulnerability (MSSQL injection)
随机推荐
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
Redis 排查大 key 的4种方法,优化必备
Uva1592 Database
Postman pre script - global variables and environment variables
GAMES202-WebGL中shader的编译和连接(了解向)
2021RoboCom机器人开发者大赛(初赛)
Collection + interview questions
Yyds dry goods inventory OSI & tcp/ip
麦斯克电子IPO被终止:曾拟募资8亿 河南资产是股东
[classic example] binary tree recursive structure classic topic collection @ binary tree
[NOIP2009 普及组] 分数线划定
Finance online homework
Yolov5 tensorrt acceleration
麥斯克電子IPO被終止:曾擬募資8億 河南資產是股東
关于Unity Inspector上的一些常用技巧,一般用于编辑器扩展或者其他
Acwing week 58
Scala function advanced
Postman manage test cases
Postman断言
Quick sort