oracle如何級聯(lián)更新 oracle 聯(lián)合更新

關(guān)于oracle級聯(lián)更新on update 的一個(gè)問題,

oracle不支持級聯(lián)更新,

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比安鄉(xiāng)網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式安鄉(xiāng)網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋安鄉(xiāng)地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。

可以用觸發(fā)器實(shí)現(xiàn):

Create Or Replace Trigger g_Cardapply_Tr

After Update Of g_State On g_Cardapply

For Each Row

Begin

Update g_Cardapplydetail a

Set a.g_State = :New.g_State

Where a.g_State = :Old.g_State;

End;

oracle 外鍵如何更新

用scott用戶打開兩個(gè)窗口

1、外鍵無索引時(shí),子表更新外鍵未提交,主表更新非子表引用的主鍵時(shí)被阻塞

會話1:

create table t1 (x int primary key);

insert into t1 values(1);

insert into t1 values(2);

insert into t1 values(3);

commit;

create table t2(y int references t1);

insert into t2 values(1);

commit;

update t2 set y=2 where y=1;

會話2:

update t1 set x=4 where x=3; //會話被阻塞

2、外鍵有索引時(shí),子表更新外鍵未提交,主表更新非子表引用的主鍵時(shí)不會被阻塞

會話1:

create index t2_index on t2(y) ; //創(chuàng)建外鍵索引

update t2 set y=2 where y=1;

會話2:

update t1 set x=4 where x=3;

已更新 1 行;//可以正常更新

3、外鍵有無索引,對于子表更新外鍵未提交,主表更新相對應(yīng)的主鍵無影響,更新主鍵的session都會被阻塞

會話1:

update t2 set y=2 where y=1;

會話2:

update t1 set x=4 where x=1; //更新子表已引用的

會話被阻塞。

會話1:

update t2 set y=2 where y=1;

會話2:

update t1 set x=4 where x=2 ; //更新子表將要引用的

會話被阻塞。――很好理解,主表要判斷是否違反約束

二、更新子表非外鍵列未提交

1、外鍵無索引,更新主表已被外鍵引用的主鍵時(shí),更新主鍵的session被阻塞

會話1:

create table t1 (x int primary key,x1 int);

insert into t1 values(1,1);

insert into t1 values(2,2);

insert into t1 values(3,3);

commit ;

create table t2(y int references t1,y1 int);

insert into t2 values(1,1);

commit ;

update t2 set y1=2 where y1=1;

會話2:

update t1 set x=4 where x=1; //更新外鍵引用的主鍵

會話被阻塞。

2、外鍵有索引,更新主表已被外鍵引用的主鍵時(shí),更新主鍵的session不會被阻塞而報(bào)約束錯(cuò)誤

會話1:

create index t2_index on t2(y);

update t2 set y1=2 where y1=1;

會話2:

update t1 set x=4 where x=1

*

ERROR 位于第 1 行:

ORA-02292: 違反完整約束條件 (SCOTT.SYS_C001607) - 已找到子記錄日志

3、外鍵無索引,更新主表未被外鍵引用的主鍵時(shí),更新主鍵的session被阻塞

會話1:

drop index t2_index;

update t2 set y1=2 where y1=1

會話2:

update t1 set x=4 where x=2;

會話被阻塞。

4、外鍵有索引,更新主表未被外鍵引用的主鍵時(shí),更新主鍵的session不會被阻塞

會話1:

create index t2_index on t2(y);

update t2 set y1=2 where y1=1;

會話2:

update t1 set x=4 where x=2;

已更新 1 行。

另外在一個(gè)主表有on delete cascade,子表沒有外鍵索引時(shí),對主表操作會級聯(lián)到子表,子表將進(jìn)行全表掃描。

總結(jié):在需要更新主鍵的情況下,最好是創(chuàng)建子表的外鍵索引。

oracle 如何實(shí)現(xiàn)多個(gè)進(jìn)程同時(shí)更新一條記錄,而不需要等待

等待事物結(jié)束才是正常

進(jìn)程1執(zhí)行了更新語句,但一直沒有提交,進(jìn)程2后來也執(zhí)行了更新語句,并先提交,最后進(jìn)程1提交。

這個(gè)例子在oracle中應(yīng)該是不能成功的,他破壞了數(shù)據(jù)的讀寫一致性。在update 語句后緊跟commit 不就能實(shí)現(xiàn)你的需求么

oracle樹形數(shù)據(jù)級聯(lián)更新

建表及數(shù)據(jù)

create?table?test

(fid?int,

parentid?int,

fpath?varchar2(100));

insert?into?test?values?(1,null,null);

insert?into?test?values?(2,1,null);

insert?into?test?values?(3,1,null);

insert?into?test?values?(4,2,null);

執(zhí)行更新語句

update?test?a?set?a.fpath=

(select?b.fpath?from

(select?fid,parentid,

substr(sys_connect_by_path(fid,'/'),2)?fpath

from???test

start??with?fid=1

connect?by?prior?fid=parentid)?b

where?a.fid=b.fid);

效果截圖

oracle 創(chuàng)建表 時(shí)設(shè)置 級聯(lián)刪除和級聯(lián)更新 的語句

外鍵只能是參照表的主鍵,所以應(yīng)該參照userid,要參照uname只能用觸發(fā)器。

create table users (userid primary key,uname unique)

go

create table board (bid primary key,bhost, foreign key(bhost) references users(userid) on delete CASCADE on update CASCADE)

Oracle join連接表 更新

Oracle中不需要用join連接更新數(shù)據(jù),連接表更新方法如下:

有以下兩張表:

根據(jù)test2表中的id和test1表中的id關(guān)聯(lián),修改test1表中name字段,語句如下:

update?test1?a?set?a.name=(select?b.name?from?test2?b?where?a.id=b.id)?where?a.id?in?(select?id?from?test2);

更新后,test1表中結(jié)果:

當(dāng)前題目:oracle如何級聯(lián)更新 oracle 聯(lián)合更新
URL分享:http://muchs.cn/article26/hjcccg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司