Linux下MySQL觸發器設定

來源:互聯網
上載者:User

在資料庫操作中常需要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;//

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.