首先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;
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