Oracle update 两表及以上关联更新,出现多值情况,不是一对一更新

oracle update left join 关联更新或删除解决方案 根据业务需求,需要从其他表的条件来更新本表的某一字段数据,以下是我的解决方案,可能是网上的其它方式有所不同。 oracle中直接执行的语句如下: begin for fn in( select t.id,t.shbj,r.shbj as shbj1 from mj_ry_sg t left join mj_ry r on t.zgh=r.zgh where t.yxbj='1' and r.yxbj='1' and r.shbj is not null ) loop update mj_ry_sg 阅读详情

为了方便起见,建立了以下简单模型,和构造了部分测试数据:
在某个业务受理子系统BSS中,

SQL 代码
  1. --客户资料表
  2. create table customers
  3. (
  4. customer_id number(8) not null, -- 客户标示
  5. city_name varchar2(10) not null, -- 所在城市
  6. customer_type char(2) not null, -- 客户类型
  7. ...
  8. )
  9. create unique index PK_customers on customers (customer_id)

由于某些原因,客户所在城市这个信息并不什么准确,但是在
客户服务部的CRM子系统中,通过主动服务获取了部分客户20%的所在
城市等准确信息,于是你将该部分信息提取至一张临时表中:

SQL 代码
  1. create table tmp_cust_city
  2. (
  3. customer_id number(8) not null,
  4. citye_name varchar2(10) not null,
  5. customer_type char(2) not null
  6. )

1) 最简单的形式

SQL 代码
  1. --经确认customers表中所有customer_id小于1000均为'北京'
  2. --1000以内的均是公司走向全国之前的本城市的老客户:)
  3. update customers
  4. set city_name='北京'
  5. where customer_id<1000

2) 两表(多表)关联update -- 仅在where字句中的连接

SQL 代码
  1. --这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别
  2. update customers a -- 使用别名
  3. set customer_type='01' --01 为vip,00为普通
  4. where exists (select 1
  5. from tmp_cust_city b
  6. where b.customer_id=a.customer_id
  7. )

3) 两表(多表)关联update -- 被修改值由另一个表运算而来

SQL 代码
  1. update customers a -- 使用别名
  2. set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
  3. where exists (select 1
  4. from tmp_cust_city b
  5. where b.customer_id=a.customer_id
  6. )
  7. -- update 超过2个值
  8. update customers a -- 使用别名
  9. set (city_name,customer_type)=(select b.city_name,b.customer_type
  10. from tmp_cust_city b
  11. where b.customer_id=a.customer_id)
  12. where exists (select 1
  13. from tmp_cust_city b
  14. where b.customer_id=a.customer_id
  15. )

注意在这个语句中,
=(
select b.city_name,b.customer_type from tmp_cust_city b
where b.customer_id=a.customer_id )
与
(
select 1 from tmp_cust_city b
where b.customer_id=a.customer_id)
是两个独立的子查询,查看执行计划可知,对b表/索引扫描了
2篇;
如果舍弃
where条件,则默认对A表进行全表
更新,但由于

SQL 代码
  1. select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id

有可能不能提供"足够多"值,因为tmp_cust_city只是一部分客户的信息,所以报错(如果指定的列--city_name可以为NULL则另当别论):

SQL 代码
  1. 01407, 00000, "cannot update (%s) to NULL"
  2. // *Cause:
  3. // *Action:

一个替代的方法可以采用:

SQL 代码
  1. update customers a -- 使用别名
  2. set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name)

或者

SQL 代码
  1. set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),'未知')


-- 当然这不符合业务逻辑了

4) 上述3)在一些情况下,因为B表的纪录只有A表的20-30%的纪录数,
考虑A表使用INDEX的情况,使用
cursor也许会比关联update带来更好的性能:

SQL 代码
  1. set serveroutput on
  2. declare
  3. cursor city_cur is
  4. select customer_id,city_name
  5. from tmp_cust_city
  6. order by customer_id;
  7. begin
  8. for my_cur in city_cur loop
  9. update customers
  10. set city_name=my_cur.city_name
  11. where customer_id=my_cur.customer_id;
  12. /** 此处也可以单条/分批次提交,避免锁表情况 **/
  13. -- if mod(city_cur%rowcount,10000)=0 then
  14. -- dbms_output.put_line('----');
  15. -- commit;
  16. -- end if;
  17. end loop;
  18. end;


5) 关联update的一个特例以及性能再探讨
在oracle的
update语句语法中,除了可以update表之外,也可以是视图,所以有以下1个特例:

SQL 代码
  1. update (select a.city_name,b.city_name as new_name
  2. from customers a,
  3. tmp_cust_city b
  4. where b.customer_id=a.customer_id
  5. )
  6. set city_name=new_name


这样能避免对B表或其索引的2次扫描,但前提是 A(customer_id) b(customer_id)必需是unique index或primary key。否则报错:

SQL 代码
  1. 01779, 00000, "cannot modify a column which maps to a non key-preserved table"
  2. // *Cause: An attempt was made to insert or update columns of a join view which
  3. // map to a non-key-preserved table.
  4. // *Action: Modify the underlying base tables directly.


6)oracle另一个常见错误
回到3)情况,由于某些原因,tmp_cust_city customer_id 不是唯一index/primary key

SQL 代码
  1. update customers a -- 使用别名
  2. set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
  3. where exists (select 1
  4. from tmp_cust_city b
  5. where b.customer_id=a.customer_id
  6. )

当对于一个给定的a.customer_id
(
select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
返回多余
1条的情况,则会报如下错误:

SQL 代码
  1. 01427, 00000, "single-row subquery returns more than one row"
  2. // *Cause:
  3. // *Action:

一个比较简单近似于不负责任的做法是(不能说是不是负责的做法,若是用字表的值更新主表,那么应该找到一个合适的业务时间字段或者其他业务字段做排序后取rownum=1)

SQL 代码
  1. update customers a -- 使用别名
  2. set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id and rownum=1)

如何理解 01427 错误,在一个很复杂的多表连接update的语句,经常因考虑不周,出现这个错误,
仍已上述例子来描述,一个比较简便的方法就是将A表代入 值表达式 中,使用
group by 和
having 字句查看重复的纪录 

SQL 代码
  1. (select b.customer_id,b.city_name,count(*)
  2. from tmp_cust_city b,customers a
  3. where b.customer_id=a.customer_id
  4. group by b.customer_id,b.city_name
  5. having count(*)>=2
  6. )
ORACLE_多表关联_UPDATE_语句 把其它表作为条件和更新值的更新语句;例如: 两表(多表)关联update -- 仅在where字句中的连接 --这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别 update customers a -- 使用别名 set customer_type='01' --01 为vip,00为普通 where exists (select 1 from tmp_cust_city b where b.customer_id=a.customer_id ) 立即下载

相关推荐

ORACLE多表关联的update语句

在某个业务受理子系统BSS中, --客户资料表 create table customers ( customer_id number(8) not null, -- 客户标示 city_name varchar2(10) not null, -- 所在城市 customer_type char(2) not null, -- 客户类型 ... ) create unique index PK_customers on customers (customer_id) 由于某些原因,客户所在城市这个信息并不什么准确,但是在 客户服务部的CRM子系统中,通过主动服务获取了部分客户20%的所在 城市等准确信息,于是你将该部分信息提取至一张临时表中: create table tmp_cust_city ( customer_id number(8) not null, citye_name varchar2(10) not null, customer_type char(2) not null )

Oracle表关联更新几种方法

Oracle表关联更新

zxrhhm的博客 1万+

钙钛矿太阳能电池稳定性测试:ISOS协议详解与标准化实践指南

在光伏器件研发中,稳定性测试是评估材料与器件长期可靠性的核心环节,其关键在于实现测试条件的标准化与数据的可比性。通过控制变量和模拟真实环境应力,标准化测试协议能够系统揭示材料衰减机理与器件失效模式,为技术迭代提供可靠依据。在钙钛矿太阳能电池领域,ISOS协议作为国际公认的测试框架,通过分级体系(如ISOS-L系列光照测试与ISOS-D系列暗态测试)和关键参数定义(如光源光谱匹配、MPP跟踪、结温控制),解决了不同实验室数据无法横向对比的痛点。该协议不仅规范了测试流程,还通过硬件系统搭建、自动化数据采集与归一

weixin_33600816的博客 346

Oracle数据库update语句用法,多表批量更新对应的字段值

Oracle数据库update语句用法,多表批量更新对应的字段值,日常工作经常会遇到参照某个表格,更新主表对应字段的值 一般可以用excel的VLOOKup函数进行查找匹配,但是这种方法需要将表从数据库中导出,更新完了之后再导回数据库中。 我们用update语句可以很方便在数据库里完成更新。

jasminesolo的博客 1万+

Android车载多屏开发实战:从零搭建同显/异显Demo(附源码)

本文详细介绍了Android车载多屏开发的实战经验,从零搭建同显/异显Demo,涵盖多屏模拟、核心API调用、同显/异显架构实现及性能优化等关键环节。通过C++构建完整开发链路,帮助开发者快速掌握多屏同显和多屏异显技术,提升智能座舱系统的交互体验。附源码供参考。

weixin_29233857的博客 413

Autosar中CAN信号传输的模块化解析与实战应用

本文深入解析了AUTOSAR架构下CAN信号传输的模块化流程,详细阐述了从应用层软件组件(SWC)到COM模块打包、PduR路由、CanIf硬件抽象直至Can Driver驱动的完整“流水线”。文章通过实战案例,重点说明了模块化设计如何实现解耦与可移植性,并提供了关键模块配置要点与调试技巧,旨在帮助开发者构建高可靠、实时的车载通信系统。

g9h0i1的博客 911

oracle update 多表关联更新

oracle 多表关联更新 update t_water_livestock_breed_2020 t set t.type_identified = (select r.iteam_code from t_dict_dictionary_iteam r where t.type_identified = r.iteam_name and r.class_id = '1fce77fea4764357b15e3d2a40507857' ) where ex

JERRY_CHEN9999 1万+

避坑指南:华为2288H-V5服务器安装系统时常见的RAID与驱动问题解决方案

本文针对华为2288H-V5服务器在安装系统时常见的RAID与驱动问题,提供了详细的避坑指南。文章深度解析了从BIOS版本兼容性核查、RAID配置实战到驱动加载与集成的全流程,重点解决了安装过程中因RAID控制器驱动缺失或兼容性问题导致的硬盘无法识别等典型故障,帮助用户高效完成系统部署。

weixin_29240711的博客 1010

Oracle多表关联更新

这里写目录标题Oracle多表关联更新一.最简单的形式-单表更新二.两表(多表)关联update -- set为简单的数据(直接是值),且仅在where字句中的连接三.两表(多表)关联update -- 被修改值由另一个表运算而来四.内联视图更新 与 merge更新五.特殊情况的优化:六.关联update的一个特例以及性能再探讨 Oracle多表关联更新 更新原则: 1)更新的时候一定要加where条件,否则必然引起该字段的所有记录更新 2)跨表更新时,set和where时,尽量减少扫描次数,从而提高优化

想飞的孤独少年的博客 1万+

【ESP32】SPI SRAM memory test fail。External RAM failed memory test!

log日志报错如下:这两条报错信息表明外部 SPI SRAM(静态随机存取存储器)的内存测试失败。

weixin_56217406的博客 1027

华为OD机考双机位C卷 - 虚拟理财游戏(Java & Python& JS & C/C++ & GO )

华为OD机考双机位C卷 - 虚拟理财游戏(OD上机考试双机位C卷)。提供Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。

算法大师! 672

oracle多表联合查询更新,ORACLE 两表关联更新三种方式

不多说了,我们来做实验吧。创建如下表数据select * from t1 ;select * from t2;现需求:参照T2表,修改T1表,修改条件为两表的fname列内容一致。方式1,update常见陷阱:UPDATE T1SET T1.FMONEY = (select T2.FMONEY from t2 where T2.FNAME = T1.FNAME)执行后T1结果如下:有一行原有值,被...

weixin_36438124的博客 8658

成功解决raise Py4JError py4j.protocol.Py4JError: py4j.protocol.Py4JError: Could not find py4j jar at 

成功解决raise Py4JError("Could not find py4j jar at {0}".format(jarpath)) py4j.protocol.Py4JError: Could not find py4j jar at 目录 解决问题 解决思路 解决方法 1、第一步安装jdk 2、第二步安装或升级py4j 解决问题 raise Py4JError("Could not find py4j jar at {0...

头部AI社区如有邀博主AI主题演讲请私信—心比天高,仗剑走天涯,保持热爱,奔赴向梦想!低调,专注,谦虚,自律,反思,成长,还算比较正能量的博主,公益免费传播…内心特别想在AI界做出一些可以推进历史进程影响力的技术(兴趣使然,有点小情怀,也有点使命感呀 4632

Oracle两表关联更新

Oracle两表关联更新

qq_38380338的博客 1万+

英飞凌TC3xx之一起认识GTM(八)如何使用DTM实现死区时间配置

本文简要介绍英飞凌TC3xx系列芯片的DTM模块结构,以及有哪些相关寄存器可用于实现相关配置。

无所适从的我的博客 4665

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

Oracle 多表关联update

多表关联执行update1.仅在where子句中连接--table2中全部为优秀学生,将table1中的优秀学生的成绩更新为优 update table1 t1 set t1.grade = '优' where exists ( select 1 from table2 t2 where t2.id = t1.id );2.update的值来自另一个表看完以下例子,你就会明白了。示例:表t...

Lime的博客 9832

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

SQL/Oracle 两表关联更新

有TA, TB两表,假设均有三个栏位id, name, remark. 现在需要把TB表的name, remark两个栏位通过id关联,更新到TA表的对应栏位。 建表脚本: drop table TA; create table TA ( id number not null, name varchar(10) not null, remark varchar(10) not null )

Cassaba的专栏 2万+

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

UPDATE与多表关联更新

一条Update更新语句是不能更新多张表的,除非使用触发器隐含更新。而表的更新操作中,在很多情况下需要在表达式中引用要更新的表以外的数据。我们先来讨论根据其他表数据更新你要更新的表

Aqiunuoze的博客 8054

Oracle多表关联update优化

       我们都知道,在Oracle数据库中,对单表进行更新采用标准update语法效率是非常高的,语法为UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值,如下SQL所示:  update t_join_situation set join_state = '1' where year='2011'       如果涉及关联表,并把一个表的字段值更新到另一个表中...

WeiWuwen 3299

update更新多表总结

oracle数据库:update更新多表总结 总体原则:1)更新的时候一定要加where条件,否则必然引起该字段的所有记录更新 2)跨表更新时,set和where时,尽量减少扫描次数,从而提高优化 update更新实例: 最简单的形式-单表更新 SQL 代码 –经确认customers表中所有customer_id小于1000均为’北京’ –1000以内的均是公司走向全国之前的本城市的老客户:...

qq_43211632的博客 1157

两表(多表)关联update

<br />两表(多表)关联update -- 仅在where字句中的连接<br />     --这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别<br />     update customers    a         -- 使用别名<br />     set      customer_type='01' --01 为vip,00为普通<br />     where    exists (select 1<br />                    from     tmp

thy822的专栏 1495

oracle update两表关联更新,oracle 两表关联的update操作

create table a(no number notnull,name varchar(10) notnull, locvarchar(10) notnull);create table b( no number notnull, name varchar(10) notnull, loc varchar(10) notnull);insert into a values(10, 'A...

weixin_42133415的博客 6218

Oracle多表关联更新(update多表关联)

Oracle没有update from语法,可以通过两种实现方式: 1、利用子查询:      update    A        SET    字段1=(select    字段表达式    from    B    WHERE    ...),          字段2=(select    字段表达式    from    B    WHERE    ...)

it_taojingzhan的博客 3346

oracle更新多个表,ORACLE的UPDATE更新多表

关于ORACLE的UPDATE更新多表的问题有以下几种方式可以实现:一种是:update table1set (field1,field2...) =(Select Field1,field2....from table2where table1.field1=table2.field1)where table1.field1 in (select field1 from table2)二种是:将...

weixin_39724793的博客 5408
上一篇: 数据仓库主题设计及元数据设计
下一篇: eclipse常用开发插件
zhangzhongzhong
博客等级 码龄19年 58粉丝 30原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值