//说明.当我删除或者修改cat_tb的时候,sub_tb对于的外键也随着改变
功能要求:用触发器去实现这个功能
代码如下:
create table cat_tb
(
cat_id int primary key,
cat_name varchar(20)
)
go
insert into cat_tb values(100,'数码电子')
insert into cat_tb values(200,'化妆品')
insert into cat_tb values(300,'家具')
go
create table sub_tb
(
sub_id int primary key,
sub_name varchar(20),
cat_id int foreign key(cat_id) references cat_tb
)
go
insert into sub_tb values(1000,'电脑',100)
insert into sub_tb values(2000,'鼠标',100)
insert into sub_tb values(3000,'美容品',200)
insert into sub_tb values(4000,'桌子',300)
insert into sub_tb values(5000,'凳子',300)
go
create trigger cat_sub
on cat_tb
after delete,update
as
if exists(select 1 from inserted) and exists(select 1 from deleted)
begin
update sub_tb set cat_id=(select cat_id from inserted) where cat_id=(select cat_id from deleted)
print '修改成功 '
end
else if exists(select 1 from deleted)
begin
delete sub_tb from deleted where sub_tb.cat_id=(select deleted.cat_id from deleted)
print '删除成功'
end
create table cat_tb
(
cat_id int primary key,
cat_name varchar(20)
)
go
insert into cat_tb values(100,'数码电子')
insert into cat_tb values(200,'化妆品')
insert into cat_tb values(300,'家具')
go
create table sub_tb
(
sub_id int primary key,
sub_name varchar(20),
cat_id int foreign key(cat_id) references cat_tb(cat_id) ON DELETE CASCADE ON UPDATE CASCADE
)
go
DROP TABLE sub_tb,cat_tb
create table cat_tb
(
cat_id int primary key,
cat_name varchar(20)
)
go
insert into cat_tb values(100,'数码电子')
insert into cat_tb values(200,'化妆品')
insert into cat_tb values(300,'家具')
go
create table sub_tb
(
sub_id int primary key,
sub_name varchar(20),
cat_id int foreign key(cat_id) references cat_tb
)
go
insert into sub_tb values(1000,'电脑',100)
insert into sub_tb values(2000,'鼠标',100)
insert into sub_tb values(3000,'美容品',200)
insert into sub_tb values(4000,'桌子',300)
insert into sub_tb values(5000,'凳子',300)
go
CREATE trigger cat_sub
on cat_tb
INSTEAD OF delete,update
as
if exists(select 1 from inserted) and exists(select 1 from deleted)
begin
update sub_tb set cat_id=null where cat_id=(select TOP 1 cat_id from deleted)
UPDATE cat_tb SET cat_id=(select TOP 1 cat_id from inserted) where cat_id=(select TOP 1 cat_id from deleted)
update sub_tb set cat_id=(select TOP 1 cat_id from inserted) where cat_id IS NULL
print '修改成功 '
end
else if exists(select 1 from deleted)
begin
delete sub_tb where EXISTS(select 1 from DELETED WHERE cat_id=sub_tb.cat_id)
DELETE cat_tb WHERE EXISTS(SELECT 1 FROM DELETED WHERE cat_id=cat_tb.cat_id)
print '删除成功'
end
GO
update cat_tb set cat_id=400 where cat_id=300
go
SELECT * FROM sub_tb--查看結果
/*
SELECT * FROM sub_tb
*/
DROP TABLE sub_tb,cat_tb
--> 测试数据: cat_tb
if object_id('cat_tb') is not null drop table cat_tb
go
create table cat_tb
(
cat_id int primary key,
cat_name varchar(20)
)
go
insert into cat_tb values(100,'数码电子')
insert into cat_tb values(200,'化妆品')
insert into cat_tb values(300,'家具')
go
if object_id('sub_tb') is not null drop table sub_tb
go
create table sub_tb
(
sub_id int primary key,
sub_name varchar(20),
cat_id int constraint FK1 --稍微改动
foreign key(cat_id) references cat_tb
)
--alter table sub_tb drop constraint fk1
go
insert into sub_tb values(1000,'电脑',100)
insert into sub_tb values(2000,'鼠标',100)
insert into sub_tb values(3000,'美容品',200)
insert into sub_tb values(4000,'桌子',300)
insert into sub_tb values(5000,'凳子',300)
go
if object_id('cat_sub') is not null drop trigger cat_sub
go
create trigger cat_sub
on cat_tb
after delete,update
as
if exists(select 1 from inserted) and exists(select 1 from deleted)
begin
update sub_tb set cat_id=(select cat_id from inserted) where cat_id=(select cat_id from deleted)
print '修改成功 '
end
else if exists(select 1 from deleted)
begin
delete sub_tb from deleted where sub_tb.cat_id=(select deleted.cat_id from deleted)
print '删除成功'
end
go
ALTER TABLE sub_tb NOCHECK CONSTRAINT FK1
update cat_tb set cat_id=400 where cat_id=300
ALTER TABLE sub_tb CHECK CONSTRAINT FK1
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'--禁用约束
EXEC sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL' --启用约束
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。