oracle关联更新语法 |
您所在的位置:网站首页 › oracleupdate更新不了 › oracle关联更新语法 |
oracle 关联更新语法 oracle 更新语法: 1. ⼀般语法 update tab set col = .... [where ...] = 后可以有⼦查询,但是必须对于 tab 的每⼀列返回唯⼀⼀⾏ 与之对应, where 是需要更新的表,部分更新必须加,否则相关⼦查询的更新会把没有匹配的更 新为 null, 如 update tab a set a.col=(select b.col from b where a.id=b.id) where exists (select 1 from b where a.id=b.id) 类似地写了多遍 2. 改进语法 merge merge into tab using ( 表 | 视图 | ⼦查询等 ) -- ⼦查询需要加括号 when match then do update when no match then do insert insert 语法和 update 语法有所不同,详细参考⽂档, 10g 还⽀持 update,insert 的有条件更新和插 ⼊,⽀持 update 的 delete where, ⽀持只有 update 或 insert 的 不能修改 using ⾥的关联列,同样,必须每⼀⾏有唯⼀与之对应的 上⾯两种语法如果找不到唯⼀对应的,需要改进语句,⽐如加 rownum=1 3.update inline view 的⽤法 update (select ........... 关联查询 ) set ⽬标 = 源
如 update(select a.name,b.name from a,b where a.id=b.id) set a.name=b.name;
需要 unique 建保证唯⼀对应,⽐如上⾯的必须要 b.id 有唯⼀键,也就是 preserved key, ⽐如唯 ⼀索引什么的都可以, 11g 之前可以⽤ hint: bypass_ujvc ,这样不需要唯⼀键,但是可能有问 题,⼀对多会更新多次, 11g 这个 hint 失效 delete (select ....) 也可以,有很多要求,可以看 sql ⽂档, insert (select ...) 限制更多
第 3 种⽅法来源于可更新的视图 oracle 更新基本有 3 种 sql 写法,后⾯两种往往优化中会使⽤到,特别第⼀种的更新关联⼦查询中 源表不⾛索引,那么更新很多,相当于 nested loop, 肯定慢,⽽且还有个 where 过滤,多次访问 源表 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |