我有一张Absence
带 PK的桌子AbscenceId
(是的拼写错误)。我有另一个 table Note
,带有一个 FK intoAbsence
调用AbsenceId
(这次拼写正确)。FK 约束为:
ALTER TABLE [dbo].[Note] WITH CHECK ADD CONSTRAINT [FK_Note_Absence] FOREIGN KEY([AbsenceId])
REFERENCES [dbo].[Absence] ([AbscenceId])
GO
当 anAbsence
被删除时,我希望所有人都Notes
可以使用它。我不能这样做CASCADES
,因为 aNote
也可以属于其他东西。
所以我创建了一个触发器来在删除缺席时删除笔记:
ALTER TRIGGER [dbo].[TR_OnAbsenceDelete] ON [dbo].[Absence]
FOR DELETE AS
DELETE FROM [Note]
WHERE AbsenceId IN ( SELECT AbscenceId FROM Deleted )
但是当我删除Absence
一个Note
附加的,我得到:
Msg 547, Level 16, State 0, Line 1
The DELETE statement conflicted with the REFERENCE constraint "FK_Note_Absence". The conflict occurred in database "ReturnToWork", table "dbo.Note", column 'AbsenceId'.
The statement has been terminated.�
这几乎就像触发器没有被执行?