我理解触发器的想法,但对如何使用它来施加约束有点困惑。例如,我有两个表:一个student
表和一个book_order
表,显示学生订购的书。
我想创建一个触发器,用于检查带有图书订单的学生是否无法从学生表中删除。
答案 0 :(得分:0)
不确定为什么你会这样做,除非你想写日志或事情发生但是......
create TRIGGER Del_Student
ON dbo.Student
INSTEAD OF DELETE
AS
BEGIN
Declare @cnt int
Select @cnt = count(*) from deleted d
Inner Join BookOrders o on d.studentId = o.studentId
if (@cnt > 0)
BEGIN
RAISERROR ('Deletions not allowed from this table when bookorders exist for student)', 16, 1)
END
END