前言
今天敲牛腩需要建立触发器,但是我的sql sever2014中新建触发器是灰色的,不能新建,百度了两种方法,可以在新建查询中写sql语句,另一种方式是使用Navicat Premium写简单的语句去建立。
sql语句创建触发器
create trigger trigCategoryDelet //创建触发器的名称
ON [dbo].[category] //在哪个表中创建
for delete //触发删除命令时的触发器
as
begin
-- type your statements here
declare @caId int
select @caId=id from deleted
--删除评论
delete comment where newsId in (select newsId from news where caId=@caId)
--删除新闻
delete news where caId=@caId
--删除类别
delete category where id=@caId
END
Navicat创建触发器步骤
打开Navicat,点击连接-sql sever,填写信息,点击确定即可如图: