Oracle 多表关联更新
drop table course;
create table course (
id integer,
teacherNo integer,
teacherDesc varchar2(100),
teacherName varchar2(50),
courseName varchar2(50)
);
insert into course values(1,100,'Mr.zhang','ZhangSan','English');
insert into course values(2,101,'Mr.wang','WangWu','History');
insert into course values(2,101,'Mr.wang','WangWu','Chinese'); update course set teacherDesc='Good Teacher' where teacherNo=101;
commit;
drop table teacher;
create table teacher(
id integer,
teacherDesc varchar2(100),
teacherName varchar2(50)
);
insert into teacher values(100,'Mr.zhang','ZhangSan');
insert into teacher values(101,'Mr.wang','WangWu');
update teacher set teacherDesc='Excellent Teacher' where id=101;
commit;
select c.teacherdesc, c.teachername, t.teacherdesc, t.teachername
from course c, teacher t
where c.teacherno = t.id
and (c.teacherdesc != t.teacherdesc or c.teachername != t.teachername); update course c
set (c.teacherdesc, c.teachername) =
(select t.teacherdesc, t.teachername
from teacher t
where c.teacherno = t.id
and (c.teacherdesc != t.teacherdesc or
c.teachername != t.teachername))
where exists (select 1
from teacher t
where c.teacherno = t.id
and (c.teacherdesc != t.teacherdesc or
c.teachername != t.teachername));
commit;
select c.teacherdesc, c.teachername, t.teacherdesc, t.teachername
from course c, teacher t
where c.teacherno = t.id
and (c.teacherdesc != t.teacherdesc or c.teachername != t.teachername); select c.teacherdesc, c.teachername, t.teacherdesc, t.teachername
from course c, teacher t
where c.teacherno = t.id;

update时报ORA-01779:
数据准备:
CREATE TABLE test1 ( id integer primary key, num integer );
INSERT INTO test1 VALUES (1,0);
INSERT INTO test1 VALUES (2,0);
INSERT INTO test1 VALUES (3,0);
INSERT INTO test1 VALUES (4,0); CREATE TABLE test2 ( id integer, num integer, upd integer );
INSERT INTO test2 VALUES (1,10, 0);
INSERT INTO test2 VALUES (2,20, 1); commit;
执行如下更新语句会报错:ORA-01779: 无法修改与非键值保存表对应的列
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 view which
// map to a non-key-preserved table.
// *Action: Modify the underlying base tables directly.
UPDATE (SELECT T1.ID ID1, T1.NUM NUM1, T2.ID ID2, T2.NUM NUM2
FROM TEST1 T1, TEST2 T2
WHERE T1.ID = T2.ID
AND T2.UPD = 1)
SET NUM1 = NUM2;
这个错误的意思是,子查询的结果中,更新数据源(test2)的内容不唯一,导致被更新对象(test1)中的一行可能对应数据源(test2)中的多行。
本例中,test2表的id不唯一,因此test2表中可能存在id相同但是num不相同的数据,这种数据是无法用来更新 test1 的。【这个报错属于事前检查,没有通过校验】
解决方法就是保证数据源的唯一性,例如本例中可以为test2.id创建一个唯一索引:
CREATE UNIQUE INDEX test2_idx_001 ON test2 (id);
之后上面的更新就可以执行了。
也可以使用如下命令来使用test2中记录具有唯一性
alter table test2 modify id unique;
另外也可以强制 Oracle 执行,方法是加上 BYPASS_UJVC 注释。
UPDATE (SELECT /*+ BYPASS_UJVC */
T1.ID ID1, T1.NUM NUM1, T2.ID ID2, T2.NUM NUM2
FROM TEST1 T1, TEST2 T2
WHERE T1.ID = T2.ID
AND T2.UPD = 1)
SET NUM1 = NUM2;
BYPASS_UJVC的作用是跳过Oracle的键检查。
这样虽然能够执行了,但是如果test2中存在不唯一的数据,test1就会被更新多次而导致意想不到的结果。【有风险,不建议使用】
http://www.linuxidc.com/Linux/2012-08/69089.htm
Oracle 多表关联更新的更多相关文章
- ORACLE 两表关联更新三种方式
不多说了,我们来做实验吧. 创建如下表数据 select * from t1 ; select * from t2; 现需求:参照T2表,修改T1表,修改条件为两表的fname列内容一致. 方式1,u ...
- Oracle\MS SQL Server Update多表关联更新
原文:Oracle\MS SQL Server Update多表关联更新 一条Update更新语句是不能更新多张表的,除非使用触发器隐含更新.而表的更新操作中,在很多情况下需要在表达式中引用要更新的表 ...
- oracle多表关联删除数据表记录方法
oracle多表关联删除的两种方法 第一种使用exists方法 delete from tableA where exits ( select 1 from tableB Where tableA.i ...
- MySQL 多表关联更新及删除
目录: <MySQL中的两种临时表> <MySQL 多表关联更新及删除> <mysql查询优化之三:查询优化器提示(hint)> 一. 多表关联更新 问题 ...
- oracle 两表关联查询
oracle 两表关联查询 CreationTime--2018年7月4日17点27分 Author:Marydon 情景描述 查询学生表student,sname,sex,age信息及所在班级c ...
- oracle多表关联查询和子查询
oracle多表关联查询和子查询 一.多表关联查询 例子: SQL> create table student1 ( sid ), sname ), sage )); Table created ...
- oracle多表关联删除的两种方法
oracle多表关联删除的两种方法 第一种使用exists方法 delete from tableA where exits ( select 1 from tableB Where tableA.i ...
- 数据库MySQL中关于“多表关联更新”的那些事
在常见的sql中,我们经常在查询中进行多表关联查询,用的比较熟练.今天在开发中遇到一个实际业务场景是多表关联更新,一时不知所措.本着多学习的态度,没有直接写java代码去实现,终于把多表关联更新的sq ...
- Oracle SQL性能优化 - 根据大表关联更新小表
需求: 小表数据量20w条左右,大表数据量在4kw条左右,需要根据大表筛选出150w条左右的数据并关联更新小表中5k左右的数据. 性能问题: 对筛选条件中涉及的字段加index后,如下常规的updat ...
随机推荐
- 【 D3.js 入门系列 --- 4 】 怎样使用scale(比例)
本人的个人博客为: www.ourd3js.com csdn博客为: blog.csdn.net/lzhlzz 转载请注明出处,谢谢. 在上一节中使用了一个非常重要的概念 - scale (这个不知道 ...
- C++ map简单运用
#include <iostream> #include <string> #include <map> using namespace std; typedef ...
- 设计模式——工厂模式(Factory)
要想正确理解设计模式,首先必须明白它是为了解决什么问题而提出来的. 设计模式学习笔记 --Shulin 转载请注明出处:http://blog.csdn.net/zhshulin 1.概念 工厂模式定 ...
- Android AlarmManager报警的实现
什么是AlarmManager? AlarmManager它是Android经常使用的系统-Level提醒服务,我们指定为广播中的特定时间Intent. 我们设定一个时间,然后在该时间到来时.Alar ...
- C# WinForm多线程(三)Control.Invoke
下面我们就把在Windows Form软件中使用Invoke时的多线程要注意的问题给大家做一个介绍. 首先,什么样的操作需要考虑使用多线程?总的一条就是,负责与用户交互的线程(以下简称为UI线程)应该 ...
- ps命令用法详解(转)
ps p 22763 -L -o pcpu,pid,tid,time,tname,cmd,pmem,rss --sort rss 按rss排序 ps p 26653 -L -o pcpu,tid ...
- RH033读书笔记(7)-Lab 8 Introduction to String Processing
Lab 8 Introduction to String Processing Sequence 1: Exercises in string processing 1. Other than the ...
- WIZnet通过启动在线培训活动:计算机网络TCP/IP协议而事实上,现在的方法
为了给大家营造更好的学习环境.WIZnet特此举办第一期培训活动,由WIZnet一线project师为你分享最最前沿和有用的网络技术知识,帮你解答开发过程中的疑问.欢迎前来交流.名额有限(20名满), ...
- 安装Microsoft .NET Framework 3.5 Service Pack 1回报1603错
server升级了一下系统补丁(360安装),所有发现.net无法打开网站,提示" 因为无法创建应用程序域,因此未能运行请求.错误: 0x80070002 系统找不到指定的文件. " ...
- 构造函数为什么不能为虚函数 & 基类的析构函数为什么要为虚函数
一.构造函数为什么不能为虚函数 1. 从存储空间角度,虚函数相应一个指向vtable虚函数表的指针,这大家都知道,但是这个指向vtable的指针事实上是存储在对象的内存空间的.问题出来了,假设构造函数 ...