<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Decode360's Blog

    業精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
      302 隨筆 :: 26 文章 :: 82 評論 :: 0 Trackbacks
    ??? 最近學到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...語句是可以更新成功的
    ?
    ?
    ?
    ?
    ?




    -The End-

    posted on 2009-04-06 21:51 decode360-3 閱讀(1435) 評論(2)  編輯  收藏 所屬分類: Oracle

    評論

    # re: Update的另一種寫法[未登錄] 2009-10-14 10:57 阿牛
    ^@^Oracle確實強大,這種update看似簡單,其實處理蠻麻煩的  回復  更多評論
      

    # re: Update的另一種寫法 2010-09-07 15:51 kruce lee
    @阿牛
    modifying a join view....  回復  更多評論
      

    主站蜘蛛池模板: 在线观看成人免费| 久久精品免费观看国产| 毛片免费在线观看网站| 91亚洲国产成人久久精品网站| APP在线免费观看视频| 亚洲AV无码一区二区乱孑伦AS| 丰满人妻一区二区三区免费视频 | 精品国产无限资源免费观看| 亚洲AV无码久久精品狠狠爱浪潮| 国产精品小视频免费无限app| 久久影院亚洲一区| 日韩精品无码免费专区午夜不卡| 亚洲国产精品嫩草影院在线观看| 日本免费在线观看| 亚洲高清日韩精品第一区| 最近中文字幕大全免费视频 | 全黄A免费一级毛片| 亚洲综合最新无码专区| 久久精品国产免费一区| 久久亚洲国产伦理| 精品久久久久久久久免费影院| 亚洲人成77777在线播放网站不卡| 在线看片人成视频免费无遮挡| 久久精品熟女亚洲av麻豆| 亚洲伊人久久综合影院| 久久国产乱子伦精品免费看| 亚洲欧洲自拍拍偷综合| 精品免费国产一区二区| 国产成人精品免费视频大全| 久久精品国产亚洲AV网站| 手机在线看永久av片免费| 亚洲av永久无码| 亚洲精品乱码久久久久久按摩 | 久久亚洲国产精品一区二区| 日韩精品免费一级视频| 亚洲爆乳成av人在线视菜奈实| 久久精品亚洲乱码伦伦中文| 99热免费在线观看| 爱爱帝国亚洲一区二区三区| 亚洲AV综合色区无码一区| 噼里啪啦电影在线观看免费高清|