:: -- --------sql触发器-------------------------------------------------
1 .删除
CREATE TRIGGER [ Delete_TopAlexa ] ON [ dbo ] . [ tb_WebSite ]
FOR DELETE
AS
BEGIN TRANSACTION
delete from tb_alexa where websiteid = ( select websiteid from deleted)
delete from tb_top100 where websiteid = ( select websiteid from deleted)
IF ( @@ERROR <> 0 ) -- 如果有错误
BEGIN
ROLLBACK TRANSACTION -- 回滚
END
COMMIT TRANSACTION -- 提交事务
2 .更新
create trigger Test_OK
on ok
for update
as
if update (name)
begin
update sname set sname.s_name = ok.name from ok,sname
where sname.n_id = ok.n_id
end
说明: 关键字都用大写的蓝色字体标出, 斜体字代表用户字符串
triggername-------------触发器的名字
mainTableName------主表名
childTableName-------子表名
ctbid-------------------------子表的字段
mid--------------------------主表的比较字段
::----------sqlite触发器-------------------------------------------------
1.删除
CREATE TRIGGERtriggernameAFTERDELETEONmainTableNameFOREACHROW
BEGIN
DELETEFROMchildTableNameWHEREctbid=OLD.mid;
END;
2.更新
CREATE TRIGGERtriggernameAFTERUPDATEONmainTableName FOREACHROW
BEGIN
UPDATEchildTableNameSETctbid =NEW.mid
WHERE childTableName.ctbid= OLD.mid;
END;