最近學到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, '***xx' );
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...語句是可以更新成功的
另一種尊重
上中學的時候,有一節課印象非常深刻。老師問我們如果無意闖入乙個房間,發現房間裡有一位女士正在洗澡,這時應該怎麼辦?有同學回答就當什麼也沒看見,退出房間。還有同學回答 說聲對不起!女士。然後退出去。老師笑了笑說,還有更好的答案,那就是 對不起,先生!有一對結婚多年的夫妻,有一次出差在外的妻子有一件急事...
另一種勝利
另一種勝利 written by allen lee 剛才我的扣殺,出界了5.3厘公尺。雖然很可惜,但還是出界了,請確認下吧。幹 真是的,那些任性的傢伙!但是,到最後還只顧自己網球原則的正直笨蛋,和一定要用迴旋蛇標打中單人區的笨蛋,給我們看了場好比賽啊。龍崎 海棠和幹他們雖然輸了這場比賽,但他們堅持...
另一種table排序
click on the table header to sort in ascending order.last name first name birthday siblings smith john 7 12 1978 2johnson betty 10 15 1977 4henderson ...