級聯刪除和更新

2021-04-28 14:11:24 字數 3777 閱讀 2081

1.建立表時新增on delete cascade,on update cascade

create table goods

(gid int primary key,

gname varchar(10),

gprice money

)insert into goods values(101,'nokia',1200)

insert into goods values(102,'montorl',1200)

insert into goods values(103,'philips',1200)

create table provider

(pid int primary key,

pname varchar(10),

tel varchar(10)

)create table gp

(wno int primary key,

gid int references goods(gid) on delete cascade on update cascade,

pid int references provider(pid) on delete cascade on update cascade,

ldate datetime default getdate()

)insert into provider values(1001,'nokia','88223311')

insert into provider values(1002,'philips','11223344')

insert into provider values(1003,'philips','11223344')

insert into gp values(2,101,1001,getdate())

insert into gp values(1,101,1002,getdate())

select * from gp

select * from goods

select * from provider

delete from provider where pid=1001

update goods set gid=100 where gid=101

2.儲存過程實現級聯刪除和更新

create table goods

(gid int primary key,

gname varchar(10),

gprice money

)create table provider

(pid int primary key,

pname varchar(10),

tel varchar(10)

)create table gp

(wno int primary key,

gid int references goods(gid),

pid int references provider(pid),

ldate datetime default getdate()

)select * from goods

select * from provider

select * from gp

insert into goods values(101,'nokia',1200)

insert into goods values(102,'montorl',1200)

insert into goods values(103,'philips',1200)

insert into provider values(1001,'nokia','88223311')

insert into provider values(1002,'philips','11223344')

insert into provider values(1003,'philips','11223344')

insert into gp values(2,101,1001,getdate())

insert into gp values(1,101,1002,getdate())

--級聯刪除

create proc del

@gno int,

@pno int

asdelete from gp where gid=@gno or pid=@pno

delete from goods where gid=@gno

delete from provider where pid=@pno

exec del 101,0

--級聯更新

create table goods

(gid int constraint pk_gid primary key,

gname varchar(10)

)create table gp

(wno int primary key,

gno  int constraint fk_gid foreign key references goods(gid),

price money

)insert into goods values(101,'nokia')

insert into goods values(102,'philips')

insert into goods values(103,'sansang')

insert into gp values(2,102,1200)

create proc updt

@gno int,

@gnew int

asalter table gp drop constraint fk_gid

update gp set gno=@gnew where gno=@gno

update goods set gid=@gnew where gid=@gno

alter table gp add constraint fk_gid foreign key(gno) references goods(gid)

exec updt 101,100

3.觸發器實現級聯刪除和更新

create table goods

(gid int constraint pk_gid primary key,

gname varchar(10)

)create table gp

(wno int primary key,

gno  int constraint fk_gid foreign key references goods(gid),

price money

)insert into goods values(101,'nokia')

insert into goods values(102,'philips')

insert into goods values(103,'sansang')

insert into gp values(1,101,1200)

insert into gp values(2,102,1200)

create trigger deleteupdate

on goods

for  inserted of delete

asdeclare @no int

select  @no=gid from deleted

delete from gp where gno=@no

delete from goods where gno=@no

commit

Oracle系列 級聯刪除和級聯更新

必須宣告 此部落格 於oracle外來鍵級聯刪除和級聯更新 鑑於此前收藏的精彩部落格無料被刪除了,很是痛心,所以還是要複製一下 一 級聯刪除 oracle在外鍵的刪除上有no action 類似restrict cascade和set null三種行為。下面以學生 班級為例說明不同情況下的外來鍵刪除...

Oracle外來鍵級聯刪除和級聯更新

oracle在外鍵的刪除上有no action 類似restrict cascade和set null三種行為。下面以學生 班級為例說明不同情況下的外來鍵刪除,學生屬於班級,班級的主鍵是學生的外來鍵。班級表 crate table tb class id number not null,班級主鍵 n...

Oracle外來鍵級聯刪除和級聯更新

oracle在外鍵的刪除上有no action 類似restrict cascade和set null三種行為。下面以學生 班級為例說明不同情況下的外來鍵刪除,學生屬於班級,班級的主鍵是學生的外來鍵。班級表 crate table tb class id number not null,班級主鍵 n...