linux mysql 创建触发器_Linux下MySQL触发器设置

在数据库操作中常需要delete,insert,update数据库表,在一个表发生变化时,其他表可以做相应的变化,下面就写下,今天研究了一

在数据库操作中常需要delete,,insert,update数据库表,在一个表发生变化时,其他表可以做相应的变化,下面就写下,今天研究了一天的Linux下触发器的知识:

delete -- > old

insert -- > new

update -- > old & new

1 tbl_AreaInfo

删除触发器

CREATE TRIGGER tr_ai_delete BEFORE Delete ON tbl_AreaInfo

FOR EACH ROW

delete from tbl_DataRight where tbl_DataRight.dr_AreaId=OLD.ai_Id

2 tbl_DeviceInfo

删除触发器

delimiter //

CREATE TRIGGER tr_di_delete BEFORE Delete ON tbl_DeviceInfo

FOR EACH ROW

BEGIN

delete from tbl_AlarmSubscribe where tbl_AlarmSubscribe.as_DeviceId=OLD.di_Id;

delete from tbl_CameraInfo where tbl_CameraInfo.ci_DeviceId=OLD.di_Id;

delete from tbl_FailureHistory where tbl_FailureHistory.fh_DeviceId=OLD.di_Id;

delete from tbl_AlarmInput where tbl_AlarmInput.ai_DeviceId=OLD.di_Id;

END;//

3 tbl_FailureHistory

删除触发器

CREATE TRIGGER tr_fh_delete BEFORE Delete ON tbl_FailureHistory

FOR EACH ROW

delete from tbl_ProcessHistory where tbl_ProcessHistory.ph_FailureHistoryId=OLD.fh_Id

插入触发器

delimiter //

CREATE trigger tr_fh_insert BEFORE Insert on tbl_FailureHistory

FOR EACH ROW

BEGIN

insert into tbl_ProcessHistory set ph_FailureHistoryId=new.fh_Id,

ph_ProcessStatusId=new.fh_ProcessStatusId,

ph_CreaterId=new.fh_ProcesserId,

ph_CreateTime=new.fh_ProcessTime,

ph_Memo=new.fh_ProcessMemo;

END;//

更新触发器

delimiter //

CREATE trigger tr_fh_update BEFORE Update on tbl_FailureHistory

FOR EACH ROW

BEGIN

if (old.flag = 1,new.flag = 1) then

insert into tbl_ProcessHistory set ph_FailureHistoryId=new.fh_Id,

ph_ProcessStatusId=new.fh_ProcessStatusId,

ph_CreaterId=new.fh_ProcesserId,

ph_CreateTime=new.fh_ProcessTime,

ph_Memo=new. fh_ProcessMemo;

end if;

if (old.flag = 1,new.flag = 0) then

update tbl_ProcessHistory set tbl_ProcessHistory.flag=0 where tbl_ProcessHistory.ph_FailureHistoryId=new.fh_Id;

end if;

if (old.flag = 0,new.flag = 1) then

update tbl_ProcessHistory set tbl_ProcessHistory.flag=1 where tbl_ProcessHistory.ph_FailureHistoryId=new.fh_Id;

End if;

END;//

logo.gif

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值