下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、ORACLE 多表关联 UPDATE 语句为了方便起见 ,建立了以下简单模型 , 和构造了部分测试数据 在某个业务受理子系统BSS中,- 客户资料表create tablecustomerscustomer_idnumber(8)not null ,- 客户标示city_namevarchar2(10)not null ,- 所在城市customer_type char(2)not null ,- 客户类型)create unique index PK_customers on customers (customer_id) 由于某些原因,客户所在城市这个信息并不什么准确,但是在客户服务部的C
2、RM子系统中,通过主动服务获取了部分客户20%勺所在城市等准确信息,于是你将该部分信息提取至一张临时表中: create table tmp_cust_city(customer_id number(8) not null , citye_name varchar2(10) not null , customer_type char (2) not null)1) 最简单的形式- 经确认 customers 表中所有 customer_id 小于 1000 均为 ' 北京 ' -1000 以内的均是公司走向全国之前的本城市的老客户 :)update customers setc
3、ity_name= ' 北京'where customer_id<10002) 两表(多表)关联update -仅在where字句中的连接-这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别 update customers a- 使用别名set customer_type= '01'-01 为 vip , 00 为普通where exists ( select 1from tmp_cust_city b where b.customer_id=a.customer_id)3) 两表 ( 多表) 关联 update - 被修改值由另一个表运算而来
4、update customers a - 使用别名whereset city_name=( select b.city_name from tmp_cust_city b b.customer_id=a.customer_id)where exists ( select 1from tmp_cust_city bwhere b.customer_id=a.customer_id)- update 超过 2 个值 update customers a - 使用别名set (city_name,customer_type)=( select b.city_name,b.customer_typef
5、rom tmp_cust_city bwhere b.customer_id=a.customer_ id)where exists ( select 1from tmp_cust_city bwhere b.customer_id=a.customer_id) 注意在这个语句中,=( select b.city_name,b.customer_typefrom tmp_cust_city bwhere b.customer_id=a.customer_id)与(select1fromtmp_cust_city bwhereb.customer_id=a.customer_id)是两个独立的子
6、查询,查看执行计划可知,对b 表/索引扫描了 2 篇;如果舍弃where条件,则默认对A表进行全表更 新 , 但 由 于 ( select b.city_name from tmp_cust_city b where where b.customer_id=a.customer_id)有可能不能提供 "足够多 "值,因为 tmp_cust_city 只是一部分客户的信息, 所以报错(如果指定的列-city_name可以为NULL则另当别论):01407, 00000, "cannot update (%s) to NULL"/ *Cause:/ * Ac
7、tion :一个替代的方法可以采用:update customers a - 使用别名set city_name=nvl( select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name)或者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
8、-30%的纪录数, 考虑A表使用INDEX的情况,使用cursor也许会比关联update带来更好的 性能:set serveroutput ondeclare cursor select fromcity_cur is customer_id,city_name tmp_cust_cityorder by customer_id;beginfor my_cur in city_cur loopupdate customersset city_name=my_cur.city_namewhere customer_id=my_cur.customer_id;/* 此处也可以单条 / 分批次提交
9、,避免锁表情况 */- if mod(city_cur%rowcount,10000)=0 then-dbms_output.put_line(' ');-commit;- end if;end loop;end;5) 关联 update 的一个特例以及性能再探讨在 oracle 的 update 语句语法中,除了可以 update 表之外,也可以是视图,所以有以下 1 个特例:update (select a.city_name,b.city_name as new_namefrom customers a, tmp_cust_city bwhere b.customer_i
10、d=a.customer_id)set city_name=new_name这样能避免对 B 表或其索引的 2 次扫描,但前提是 A(customer_id)b(customer_id) 必需是 unique index或 primary key 。否则报错:01779, 00000, "cannot modify a column which maps to a non key-preserved table"/ *Cause: An attempt was made to insert or update columns of a join viewwhich/ map
11、 to a non- key-preserved table./ * Action : Modify the underlying base tables directly.6) oracle 另一个常见错误回 到 3) 情 况 , 由 于 某 些 原 因 , tmp_cust_city customer_id 不 是 唯 一 index/ primary keyupdate customers a - 使用别名set city_name=( select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)wh
12、ere exists ( select 1from tmp_cust_city b where b.customer_id=a.customer_id)当对于一个给定的 a.customer_id( select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)返回多余 1 条的情况,则会报如下错误:01427, 00000, "single-row subquery returns more than one row"/ *Cause:/ * Action :一个比较简单近似于不负责任的做法是update customers a - 使用别名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 表代入 值
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 酒精发酵工风险评估与管理竞赛考核试卷含答案
- 化工安全员岗前流程考核试卷含答案
- 钻床工冲突管理测试考核试卷含答案
- 2024年海南州特岗教师招聘真题汇编附答案
- 2024年海南开放大学马克思主义基本原理概论期末考试题附答案
- 医疗保险政策解读与操作手册(标准版)
- 2024年运城市遴选公务员笔试真题汇编附答案
- 2024年许昌市遴选公务员笔试真题汇编附答案
- 2024年福州职业技术学院辅导员考试笔试题库附答案
- 2025年家电维修技术手册
- 四川藏区高速公路集团有限责任公司2026年校园招聘备考题库完美版
- 多重耐药菌医院感染预防与控制技术指南完整版
- 2026年1月浙江省高考(首考)英语试题(含答案详解)+听力音频+听力材料
- 河南新乡鹤壁安阳焦作2026年1月高三一模物理试题+答案
- 2026年食品安全快速检测仪器项目可行性研究报告
- 2026年及未来5年市场数据中国电能计量装置市场竞争格局及投资战略规划报告
- Web渗透测试与防护(虞菊花慕课版)单元设计
- 资本市场运作培训课件
- 地理信息安全在线培训考试系统题库及答案
- 高标准农田监理质量及安全管理措施
- 鲁教版(五四学制)(2012)九年级英语全册期末总复习
评论
0/150
提交评论