原文: http://my.oschina.net/neochen/blog/294354mysql
https://github.com/netkiller/mysql-zmq-plugingit
有2張表:github
表1:device, 裏面有最主要的2個字段(id, device_type, .....),代表了"設備類型"與"ID";sql
表2:alarm_information,記錄了每種設備的告警信息,其中有2個主要字段(id, device_type, ....);ui
要求:當 device中的某項被刪除的時候,alarm_information中該設備全部的告警信息所有被刪除。spa
在device中建立觸發器:.net
///////////////////////////////////////////////////////////////////////////////////////////////設計
create trigger update_alarm_information before delete on device
for each row code
begin
set @id=OLD.id; //保存被刪除設備的「id」orm
set @dtype=OLD.device_type; //保存被刪除記錄的「device_type」
delete from alarm_information where id=@id and device_type = @dtype;
end;
///////////////////////////////////////////////////////////////////////////////////////////////
若是用Navicat for Mysql建立觸發器就更加方便了
begin
set @id=OLD.id; //保存被刪除設備的「id」
set @dtype=OLD.device_type; //保存被刪除記錄的「device_type」
delete from alarm_information where id=@id and device_type = @dtype;
end;
4. 完成
示例一 delimiter // create trigger InsertUser before insert on user for each row Begin insert into user_group(uid,gid) values(new.uid,'group4444444444'); end;// delimiter ; 示例二 delimiter // create trigger InsertUser before insert on user for each row Begin IF new.Type=2 then insert into user_group(uid,gid) values(new.uid,'group4444444444'); else insert into user_group(uid,gid) values(new.uid,'group55555555555') END IF; end;// delimiter ; 示例三: delimiter // create trigger InsertUser before insert on user for each row Begin IF new.type=1 then insert into user_group(uid,gid) values(new.uid,'578d3369633b47bd9c1fe8bf905cbfb1'); END IF; IF new.type=2 then insert into user_group(uid,gid) values(new.uid,'387bcd57fc5a4c3c9de83ee210fef661'); END IF; end;// delimiter ;