mysql级联更新oracle_oracle级联更新与级联删除

Oracle级联删除:可以使用外键约束来实现,建立表的主外键关系,给列设置级联删除。如下:

——创建了CLASS表,并设置ID字段为主键。

835dbf8c37719b9d0e49f294ac9b7b5f.gif

--Create tablecreatetableCLASS

(

ID VARCHAR2(2) notnull,

CLASS_NAME VARCHAR2(20)

)

altertableCLASS

addconstraintPK_CLASS primarykey(ID)

c058813c06e235ebe364de88399b7a28.gif

——创建了STUDENTS表,并设置ID字段为主键,CLASS_ID为外键且有级联删除。

b28715a31ef63021813bc4217156bff5.gif

--Create tablecreatetableSTUDENTS

(

ID VARCHAR2(4) notnull,

CLASS_ID VARCHAR2(2) notnull,

STU_NAME VARCHAR2(20),

STU_AGE NUMBER)

altertableSTUDENTS

addconstraintPK_STU primarykey(ID)

altertableSTUDENTS

addconstraintFK_STU foreignkey(CLASS_ID)

referencesCLASS (ID) ondeletecascade;

515053ea9f1374069d0915811cc53da0.gif

这样删除了班级ID,所属的学生都会被删除。(转自https://www.cnblogs.com/milo-xie/archive/2011/07/17/2108939.html)

级联更新:只能使用触发器来实现,如下:

--首先创建实例表book和type

create table type(

tid number(4) primary key,

tname varchar2(10) not null

)

/

create table book(

bid number(4) primary key,

bname varchar2(20) not null,

tid number(4),

)

/

--建立外键约束

alter table book add constraint book_type foreign key(tid) references type(tid);

--插入测试数据

insert into type values(1,'历史');

insert into type values(2,'文学');

insert into book values(1,'红楼梦',2);

insert into book values(2,'西游记',2);

select * from type;

select * from book;

--创建级联更新触发器

create or replace trigger tri_type

after update of tid on type

for each row

begin

if(:old.tid<>:new.tid) then

update book set tid=:new.tid where tid=:old.tid;

end if;

end;

--进行更新操作,测试触发器是否起作用

update type set tid=3 where tid=2;

(转自http://blog.sina.com.cn/s/blog_8e5087d10102wgh6.html)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值