Oracle表关联更新几种方法

1、测试表及数据准备

sql 复制代码
create table T_update01(ID int ,infoname varchar2(32),sys_guid varchar2(36));
create table T_update02(ID int ,infoname varchar2(32),sys_guid varchar2(36));

insert into T_update01
select 1,N'1_updateName',sys_guid() from dual
union
select 2,N'2_updateName',sys_guid() from dual;
commit;

insert into T_update02
select 1,N'update_set_exists',sys_guid() from dual;
insert into T_update02
select 2,N'update_set_cursor',sys_guid() from dual;
insert into T_update02
select 3,N'3_Name',sys_guid() from dual;
commit;

-- 查询表T_update01、T_update02
select * from T_update01;

        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 1_updateName                   189F5A1099BF6606E0639C0AA8C0F15E
         2 2_updateName                   189F5A1099C06606E0639C0AA8C0F15E

select * from T_update02;

        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 update_set_exists              189F5A1099C46606E0639C0AA8C0F15E
         2 update_set_cursor              189F5A1099C56606E0639C0AA8C0F15E
         3 3_Name                         189F5A1099C66606E0639C0AA8C0F15E

2、update set column ... where exists

2.1、update set 单列字段

sql 复制代码
-- update set 单列字段,更新满足关联条件的所有数据
update T_update01 T1
set infoname=(select T2.infoname from T_update02 T2 where T2.ID=T1.ID)
where exists (select 1 from T_update02 T2 where T2.ID=T1.ID );

-- update set 单列字段 ,更新满足特定条件ID=1的数据
update T_update01 T1
set infoname=(select T2.infoname from T_update02 T2 where T2.ID=T1.ID)
where T1.ID=1;

-- 本次执行更新满足特定条件T_update01表的ID=1
SCOTT@prod02> select * from T_update01;

        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 update_set_exists              189F5A1099BF6606E0639C0AA8C0F15E
         2 2_updateName                   189F5A1099C06606E0639C0AA8C0F15E

2.2、update set 多列字段

sql 复制代码
-- T_update01表多插入一行数据
insert into T_update01
select 3,N'insert03',sys_guid() from dual;
commit;

select * from T_update01;

        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 update_set_exists              189F5A1099BF6606E0639C0AA8C0F15E
         2 2_updateName                   189F5A1099C06606E0639C0AA8C0F15E
         3 insert03                       189F5A1099C76606E0639C0AA8C0F15E

update T_update01 T1 
set (sys_guid,infoname) = (select T2.sys_guid,T2.infoname from T_update02 T2 where T2.ID=T1.ID)
where exists (select 1 from T_update02 T2 where T2.ID=T1.ID );
commit;
-- 更新后检查,sys_guid,infoname两列的值和T_update02一样了
select * from T_update01;

        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 update_set_exists              189F5A1099C46606E0639C0AA8C0F15E
         2 update_set_cursor              189F5A1099C56606E0639C0AA8C0F15E
         3 3_Name                         189F5A1099C66606E0639C0AA8C0F15E

select * from T_update02;

        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 update_set_exists              189F5A1099C46606E0639C0AA8C0F15E
         2 update_set_cursor              189F5A1099C56606E0639C0AA8C0F15E
         3 3_Name                         189F5A1099C66606E0639C0AA8C0F15E

3、使用游标

sql 复制代码
-- T_update02数据更新一下,方便使用游标更新的结果显示
update T_update02 set INFONAME='cursor is select' where id>=2;
commit;
select * from T_update02;

        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 update_set_exists              189F5A1099C46606E0639C0AA8C0F15E
         2 cursor is select               189F5A1099C56606E0639C0AA8C0F15E
         3 cursor is select               189F5A1099C66606E0639C0AA8C0F15E

-- 使用用游标更新T_update01的INFONAME字段,使其和T_update02 where id>=2
declare
  cursor cur_my_source is select infoname,id from T_update02;
  begin
     for cur_my_target in cur_my_source loop
       update T_update01 set infoname=cur_my_target.infoname where id=cur_my_target.id;
     end loop;
	 commit;
 end;
 /

-- 检查查询结果
select * from T_update01;

        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 update_set_exists              189F5A1099C46606E0639C0AA8C0F15E
         2 cursor is select               189F5A1099C56606E0639C0AA8C0F15E
         3 cursor is select               189F5A1099C66606E0639C0AA8C0F15E

4、merge into子句

sql 复制代码
create table T_merg01(ID int ,infoname varchar2(32),sys_guid varchar2(36));
create table T_merg02(ID int ,infoname varchar2(32),sys_guid varchar2(36));

insert into T_merg01
select 1,N'1_Name',sys_guid() from dual
union
select 2,N'2_Name',sys_guid() from dual;
commit;

select * from T_merg01;
        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 1_Name                         189F5A1099BB6606E0639C0AA8C0F15E
         2 2_Name                         189F5A1099BC6606E0639C0AA8C0F15E


insert into T_merg02
select 1,N'merge_into_Name1',sys_guid() from dual;

insert into T_merg02 
select 3,N'3_Name',sys_guid() from dual;

select * from T_merg02;

        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 merge_into_Name1               189F5A1099BD6606E0639C0AA8C0F15E
         3 3_Name                         189F5A1099BE6606E0639C0AA8C0F15E


merge into T_merg01 T1
using T_merg02 T2 on (T1.id=T2.id)
when matched then update set infoname=T2.infoname
when not matched then insert (ID,infoname,sys_guid) values(T2.ID ,T2.infoname,T2.sys_guid);

commit;

select * from T_merg01;
        ID INFONAME                       SYS_GUID
---------- ------------------------------ ------------------------------------
         1 merge_into_Name1               189F5A1099BB6606E0639C0AA8C0F15E
         2 2_Name                         189F5A1099BC6606E0639C0AA8C0F15E
         3 3_Name                         189F5A1099BE6606E0639C0AA8C0F15E
-- 可以发现T_merg01表的ID=1的INFONAME=merge_into_Name1和T_merg02表ID=1的值一样了
-- 可以发现T_merg01表多了一行数据是T_merg02表ID=3的这一行数据

5、Oracle 23c/AI 新特性

不论是已发版本Oracle23c free还是最终发布的长期支持的Oracle23Ai,表关联更新update和删除delete语句易用且更加优雅,类似SQLServer的关联更新

以下操作基于的环境

SQL*Plus: Release 23.0.0.0.0 - Developer-Release on Fri May 17 11:17:54 2024

Version 23.2.0.0.0

5.1、关联更新update

sql 复制代码
TESTUSER@FREEPDB1> create table t_emp as select EMPLOYEE_ID,DEPARTMENT_ID,SALARY from employees;

Table created.

TESTUSER@FREEPDB1> desc t_emp;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMPLOYEE_ID                                        NUMBER(6)
 DEPARTMENT_ID                                      NUMBER(4)
 SALARY                                             NUMBER(8,2)

TESTUSER@FREEPDB1> select * from t_emp where DEPARTMENT_ID=110;

EMPLOYEE_ID DEPARTMENT_ID     SALARY
----------- ------------- ----------
        205           110      12008
        206           110       8300

TESTUSER@FREEPDB1> update t_emp set DEPARTMENT_ID=null,SALARY=null where DEPARTMENT_ID=110;

2 rows updated.

TESTUSER@FREEPDB1> commit;

Commit complete.

TESTUSER@FREEPDB1> select * from t_emp where DEPARTMENT_ID is null;

EMPLOYEE_ID DEPARTMENT_ID     SALARY
----------- ------------- ----------
        178                     7000
        205
        206
-- oracle 23c SQL增强 表关联更新        
TESTUSER@FREEPDB1> update t_emp t1 set t1.DEPARTMENT_ID=t2.DEPARTMENT_ID,t1.SALARY=t2.SALARY from employees t2 where t2.EMPLOYEE_ID=t1.EMPLOYEE_ID and t1.DEPARTMENT_ID is null;

3 row updated.


TESTUSER@FREEPDB1> commit;

Commit complete.

TESTUSER@FREEPDB1> select t1.* from t_emp t1 where t1.DEPARTMENT_ID=110;
EMPLOYEE_ID DEPARTMENT_ID     SALARY
----------- ------------- ----------
        205           110      12008
        206           110       8300

5.2、关联删除delete

sql 复制代码
TESTUSER@FREEPDB1> delete t_emp t1 from employees t2 where t2.EMPLOYEE_ID=t1.EMPLOYEE_ID and t2.DEPARTMENT_ID=110;

45 rows deleted.


TESTUSER@FREEPDB1> commit;

Commit complete.


TESTUSER@FREEPDB1> select t1.* from t_emp t1 where t1.DEPARTMENT_ID=110;

no rows selected
相关推荐
gavin_gxh24 分钟前
ORACLE 删除archivelog日志
数据库·oracle
一叶飘零_sweeeet27 分钟前
MongoDB 基础与应用
数据库·mongodb
猿小喵43 分钟前
DBA之路,始于足下
数据库·dba
tyler_download1 小时前
golang 实现比特币内核:实现基于椭圆曲线的数字签名和验证
开发语言·数据库·golang
weixin_449310841 小时前
高效集成:聚水潭采购数据同步到MySQL
android·数据库·mysql
Cachel wood2 小时前
Github配置ssh key原理及操作步骤
运维·开发语言·数据库·windows·postgresql·ssh·github
standxy2 小时前
如何将钉钉新收款单数据高效集成到MySQL
数据库·mysql·钉钉
汪小敏同学2 小时前
【Django进阶】django-rest-framework中文文档——快速入门
网络·windows·oracle
Narutolxy3 小时前
MySQL 权限困境:从权限丢失到权限重生的完整解决方案20241108
数据库·mysql
Venchill3 小时前
安装和卸载Mysql(压缩版)
数据库·mysql