MySQL trigger settings in Linux

Source: Internet
Author: User
In database operations, delete, insert, and update databases are often required. When a table changes, other tables can be changed accordingly. Below we will write down the following:

In database operations, delete, insert, and update databases are often required. When a table changes, other tables can be changed accordingly. Below we will write down the following:

In database operations, delete, insert, and update databases are often required. When a table changes, other tables can be changed accordingly. Write down below, today I have studied the trigger knowledge in Linux for one day:

Delete --> old
Insert --> new
Update --> old & new
1 tbl_AreaInfo
Delete trigger
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
Delete trigger
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
Delete trigger
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
Insert trigger
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 ;//
Update trigger
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 ;//

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.