<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 :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
      397 隨筆 :: 33 文章 :: 29 評論 :: 0 Trackbacks
    Update的另一種寫法
    ?
    ??? 最近學到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...語句是可以更新成功的
    ?
    ?
    ?
    ?
    posted on 2009-04-06 21:51 decode360 閱讀(375) 評論(0)  編輯  收藏 所屬分類: 05.SQL
    主站蜘蛛池模板: 亚洲午夜未满十八勿入| 亚洲电影在线免费观看| 美女黄频免费网站| 在线jyzzjyzz免费视频| 2020天堂在线亚洲精品专区| 成年人视频免费在线观看| 亚洲码在线中文在线观看| 亚洲视频在线观看免费| 亚洲综合综合在线| 欧美a级在线现免费观看| 四虎亚洲精品高清在线观看| 成人看的午夜免费毛片| 亚洲精品无码av中文字幕| 哒哒哒免费视频观看在线www | 亚洲伊人久久大香线蕉啊| 在线视频免费观看爽爽爽| 亚洲老熟女五十路老熟女bbw| 国产中文字幕免费观看| 亚洲日韩在线观看免费视频| 亚洲AV无码久久| 国产成人精品免费视频大全麻豆| 色婷五月综激情亚洲综合| 日韩免费福利视频| 久久久精品国产亚洲成人满18免费网站| 国产成人精品日本亚洲专区61| 久久香蕉国产线看免费| 激情综合亚洲色婷婷五月| 免费人成在线观看网站品爱网日本 | 国产高清免费观看| 两个人的视频www免费| 亚洲小说区图片区| 四虎永久免费影院| 一区二区三区观看免费中文视频在线播放 | 亚洲AV无码成人精品区狼人影院| 一本久久综合亚洲鲁鲁五月天 | 中文字幕亚洲综合久久综合| 成人亚洲综合天堂| 蜜桃AV无码免费看永久| 国产亚洲视频在线观看网址| 亚洲AV无码成人精品区蜜桃| 好爽…又高潮了免费毛片 |