1.创建 after 融发器
(1)创建一个在插入时触发的触发器 sc_insert,当向 sc 表插入数据时,须确保插入的学号已在
student 表中存在,并且还须确保插入的课程号在 Course 表中存在﹔若不存在,则给出相应的提示
信息,并取消插入操作,提示信息要求指明插入信息是学号不满足条件还是课程号不满足条件
(注:Student 表与 sc 表的外键约束要先取消)。
create trigger sc_insert
on sc
after insert
as
if not exists (select * from student,inserted
where student.sno=inserted.sno)
begin
print '插入信息的学号不在学生表中! '
if not exists (select * from course,inserted where
course.cno=inserted. cno)
print '插入信息的课程号不在课程表中!'
rollback
end
else
begin
if not exists (select * from course,inserted where
Course.cno=inserted.cno)
begin
print '插入信息的课程号不在课程表中! '
rollback
end
end
执行:
①insert into sc
values ( '20110112','001','78')
删除外键约束;