Decode360's Blog

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

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
            302 隨筆 :: 26 文章 :: 82 評(píng)論 :: 0 Trackbacks
          ??? 最近學(xué)到update的另一種寫法,是以前自己從來(lái)沒(méi)有使用過(guò)的,看了一下文檔,也沒(méi)有找到很詳細(xì)的說(shuō)明。這種update方式其基礎(chǔ)是建立在Query中的,所以可以確保使用CBO而非RBO,可以在大表的更新時(shí)明顯得提高效率。在這里記錄一下基本的方法:
          ?
          ?
          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
          ?
          --無(wú)法Update,必須要有一個(gè)主鍵
          ?
          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
          ?
          --交換位置后依舊無(wú)法更新
          ?
          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設(shè)置主鍵后可以更新
          ?
          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
          ?
          --使用聯(lián)合主鍵也是不可以的,必須是關(guān)聯(lián)字段
          ?
          ?
          ??? 由上可知,使用這種方式來(lái)更新表,需要用于更新的表(最終數(shù)據(jù)表)的關(guān)聯(lián)字段必須設(shè)置為主鍵,且不可多字段主鍵。另外還有一個(gè)網(wǎng)友也指出了另外一個(gè)問(wèn)題:
          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).
          ?
          ??? 測(cè)試一下:
          ?
          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
          ?
          --系統(tǒng)報(bào)錯(cuò)權(quán)限不夠
          ?
          SQL> update a set a1 = (select b1 from b where b.id=a.id);
          ?
          2 rows updated.
          ?
          --使用Update...Select...語(yǔ)句是可以更新成功的
          ?
          ?
          ?
          ?
          ?




          -The End-

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

          評(píng)論

          # re: Update的另一種寫法[未登錄](méi) 2009-10-14 10:57 阿牛
          ^@^Oracle確實(shí)強(qiáng)大,這種update看似簡(jiǎn)單,其實(shí)處理蠻麻煩的  回復(fù)  更多評(píng)論
            

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

          主站蜘蛛池模板: 德昌县| 饶平县| 陈巴尔虎旗| 崇信县| 汶上县| 上思县| 加查县| 察隅县| 枣庄市| 铁岭县| 体育| 陆河县| 屏东县| 平江县| 若羌县| 乾安县| 宾阳县| 西宁市| 衡水市| 青田县| 兴化市| 宜州市| 蒲江县| 浪卡子县| 乌苏市| 尼木县| 石渠县| 雷州市| 长丰县| 台东市| 海丰县| 凤山市| 钟祥市| 呼伦贝尔市| 乐山市| 抚松县| 临泽县| 徐水县| 浏阳市| 丁青县| 平安县|