T-sql创建触发器t2,当删除departments 表中一条记录的同时删除该记录departmentid字段值在employees表中对应的记录
时间: 2024-03-07 13:47:05 浏览: 89
触发器实现sql记录
5星 · 资源好评率100%
可以使用如下T-SQL代码创建触发器t2实现删除departments表中一条记录时同时删除employees表中对应记录:
```
CREATE TRIGGER t2
ON departments
FOR DELETE
AS
BEGIN
DELETE FROM employees
WHERE departmentid IN (SELECT departmentid FROM deleted)
END
```
该触发器在departments表上创建,当该表上执行DELETE操作时,触发器会将删除的记录中的departmentid值作为筛选条件,删除employees表中departmentid与之相同的记录。注意,该触发器只会对departments表上的DELETE操作生效。
阅读全文