
创建一个触发器,不允许向Sc表中添加Student表中不存在的数据
2个回答
展开全部
首先field1是表Sc的字段,field2是表Student的字段。field1是表Sc的键。通过下面的语句可以保证掺入Sc表的数据中字段1的数值必须是表Student中字段field2中已经有了的数值。
alter table Sc add constraint cst_sc_student foreign key(field1) references Stuend(field2) on update restrict;
alter table Sc add constraint cst_sc_student foreign key(field1) references Stuend(field2) on update restrict;
展开全部
CREATE TRIGGER [IN_Sc] ON ssc
FOR insert
AS
declare @xuehao
select @xuehao= t1.xuehao
from student t1,
inserted t2
where t1.xuehao=t2.xuehao
if @xuehao is null
begin
raiserror 50001 ' 不能添加! '
rollback transaction
end
return
FOR insert
AS
declare @xuehao
select @xuehao= t1.xuehao
from student t1,
inserted t2
where t1.xuehao=t2.xuehao
if @xuehao is null
begin
raiserror 50001 ' 不能添加! '
rollback transaction
end
return
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询