资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

oracle如何级联更新 oracle 联合更新

关于oracle级联更新on update 的一个问题,

oracle不支持级联更新,

成都创新互联-专业网站定制、快速模板网站建设、高性价比安乡网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式安乡网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖安乡地区。费用合理售后完善,10余年实体公司更值得信赖。

可以用触发器实现:

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用户打开两个窗口

1、外键无索引时,子表更新外键未提交,主表更新非子表引用的主键时被阻塞

会话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、外键有索引时,子表更新外键未提交,主表更新非子表引用的主键时不会被阻塞

会话1:

create index t2_index on t2(y) ; //创建外键索引

update t2 set y=2 where y=1;

会话2:

update t1 set x=4 where x=3;

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

3、外键有无索引,对于子表更新外键未提交,主表更新相对应的主键无影响,更新主键的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、外键无索引,更新主表已被外键引用的主键时,更新主键的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、外键有索引,更新主表已被外键引用的主键时,更新主键的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=1

*

ERROR 位于第 1 行:

ORA-02292: 违反完整约束条件 (SCOTT.SYS_C001607) - 已找到子记录日志

3、外键无索引,更新主表未被外键引用的主键时,更新主键的session被阻塞

会话1:

drop index t2_index;

update t2 set y1=2 where y1=1

会话2:

update t1 set x=4 where x=2;

会话被阻塞。

4、外键有索引,更新主表未被外键引用的主键时,更新主键的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 行。

另外在一个主表有on delete cascade,子表没有外键索引时,对主表操作会级联到子表,子表将进行全表扫描。

总结:在需要更新主键的情况下,最好是创建子表的外键索引。

oracle 如何实现多个进程同时更新一条记录,而不需要等待

等待事物结束才是正常

进程1执行了更新语句,但一直没有提交,进程2后来也执行了更新语句,并先提交,最后进程1提交。

这个例子在oracle中应该是不能成功的,他破坏了数据的读写一致性。在update 语句后紧跟commit 不就能实现你的需求么

oracle树形数据级联更新

建表及数据

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);

执行更新语句

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 创建表 时设置 级联删除和级联更新 的语句

外键只能是参照表的主键,所以应该参照userid,要参照uname只能用触发器。

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连接更新数据,连接表更新方法如下:

有以下两张表:

根据test2表中的id和test1表中的id关联,修改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表中结果:


网页名称:oracle如何级联更新 oracle 联合更新
浏览地址:http://cdkjz.cn/article/hjcccg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220