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 ...
随机推荐
- sql dateDiff函数
当月的数据select * from MOPICK where dateDiff(m,getdate(),START_DATE)=0
- 【原创】ZOJ_1649 Rescue 解题报告
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- linux如何执行后台进程
linux直接执行一个过程.电流指令结束后.或者关闭掉shell形成过程将结束. 如何在后台执行的处理 办法1 采用nohup命令,nohup命令本身的意思no hung up他说,他们将不会收到sh ...
- cocos2dx环境配置和打包
安装软件准备就绪: vs2012 cocos2d-x-2.2.1 adt-bundle-windows-x86_64-20121030 android-ndk-r9c-windows-x86_64 j ...
- 关于csrss.exe和winlogon.exe进程多、占用CPU高的解决办法
原地址 http://blog.sina.com.cn/s/blog_912e77480101nuif.html 最近VPS的CPU一直处在100%左右,后台管理上去经常打不开,后来发现上远程都要 ...
- HDU 1061 Rightmost Digit解决问题的方法
求大量N^N的值最右边的数字,即最低位. 它将能够解决一个简单二分法. 只是要注意溢出,只要把N % 10之后.我不会溢出,代替使用的long long. #include <stdio.h&g ...
- hdu3182 状态压缩水题
题意是这种 给你n个汉堡 每一个汉堡有它的价值 做每一个汉堡都得花费相应的能量 如今告诉你最大能量 让你求获得的最大的价值(有些汉堡必须有还有一些汉堡做好为前提) 给你的 ...
- 了解HTML5和“她”的 API (三)
Web Workers(后台线程) JavaScript是单线程的,较长的javascript运算会阻塞UI线程. web worker 是运行在后台的 JavaScript,不会影响页面的性能. 在 ...
- axWindowsMediaPlayer1获取音频长度
OpenFileDialog openFileDialog1 = new OpenFileDialog { InitialDirectory = "c:\\", Filter = ...
- SRM 590 DIV1
转载请注明出处,谢谢viewmode=contents">http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlov ...