zoukankan      html  css  js  c++  java
  • 转:Oracle中Update的一个写法,注意异常ORA01779

      最近学到update的另一种写法,是以前自己从来没有使用过的,看了一下文档,也没有找到很详细的说明。这种update方式其基础是建立在Query中的,所以可以确保使用CBO而非RBO,可以在大表的更新时明显得提高效率。在这里记录一下基本的方法:

     
     
    SQL> create table a ( id int, a1 varchar2(25) );
    SQL> create table b ( id int, b1 varchar2(25) );
    SQL> insert into a values ( 1, 'Hello' );
    SQL> insert into a values ( 2, 'xxxxx' );
    SQL> insert into b values ( 2, 'World' );
    SQL> commit;
     
     
    SQL> update ( select a1, b1 from a, b where a.id = b.id )
      2  set a1 = b1;
    set a1 = b1
        *
    ERROR at line 2:
    ORA-01779: cannot modify a column which maps to a non key-preserved table
     
    --无法Update,必须要有一个主键
     
    SQL> alter table b add constraint b_key primary key(id);
    SQL> update ( select a1, b1 from a, b where a.id = b.id )
      2  set a1 = b1;

    1 row updated.
     

    --可以Update

     
    SQL> update ( select a1, b1 from a, b where a.id = b.id )
      2  set b1 = a1;
    set b1 = a1
        *
    ERROR at line 2:
    ORA-01779: cannot modify a column which maps to a non key-preserved table
     
    --交换位置后依旧无法更新
     
    SQL> alter table b drop constraint b_key;
    SQL> alter table a add constraint a_key primary key(id);
    SQL> update ( select a1, b1 from a, b where a.id = b.id )
      2  set b1 = a1;

    1 row updated.
     
    --为表a设置主键后可以更新
     
    SQL> alter table a drop constraint a_key;
    SQL> alter table a add constraint a_key primary key(id,a1);
    SQL> update ( select a1, b1 from a, b where a.id = b.id )
      2  set b1 = a1;
    set b1 = a1
        *
    ERROR at line 2:
    ORA-01779: cannot modify a column which maps to a non key-preserved table
     
    --使用联合主键也是不可以的,必须是关联字段
     
     
        由上可知,使用这种方式来更新表,需要用于更新的表(最终数据表)的关联字段必须设置为主键,且不可多字段主键。另外还有一个网友也指出了另外一个问题:
    If the user has update permission on table A, but only has select permission on table B, they cannot update via the first example.  Oracle will return ORA-01031 (insufficient privileges).
     
        测试一下:
     
    SQL> create user x identified by x;
    SQL> grant create session on x;
    SQL> grant select,update on a to x;

    SQL> grant select on b to x;

    SQL> create public synonym a for wangxiaoqi.a;
    SQL> create public synonym b for wangxiaoqi.b;
     
    SQL> conn x/x
    Connected.
    SQL> update ( select a1, b1 from a, b where a.id = b.id )
      2  set a1 = b1;
    update ( select a1, b1 from a, b where a.id = b.id )
                                   *
    ERROR at line 1:
    ORA-01031: insufficient privileges
     
    --系统报错权限不够
     
    SQL> update a set a1 = (select b1 from b where b.id=a.id);
     
    2 rows updated.
     
    --使用Update...Select...语句是可以更新成功的
     
     
     
     
     
    魔兽就是毒瘤,大家千万不要玩。
  • 相关阅读:
    0.1+0.2==0.3?
    Tomcat 初探(三)多项目部署
    Tomcat 初探(二) server.xml 配置
    Tomcat 初探(一) 简介
    如何使用 Idea 远程调试 Java 代码
    js-将时间戳转为正常的日期
    VUE-基本的写法
    HTML-列表无缝滚动效果2(显示一个暂停3秒,滚走显示下一个)
    JS-判断用户使用的终端是安卓还是IOS
    JS-在本页面监听是否用户是点击了返回进入的(IOS返回页面时,页面未初始化)
  • 原文地址:https://www.cnblogs.com/tracy/p/1713607.html
Copyright © 2011-2022 走看看