原文: http://my.oschina.net/neochen/blog/294354
https://github.com/netkiller/mysql-zmq-plugin
有2张表:
表1:device, 里面有最主要的2个字段(id, device_type, .....),表明了"设备类型"与"ID";
表2:alarm_information,记录了每种设备的告警信息,其中有2个主要字段(id, device_type, ....);
要求:当 device中的某项被删除的时候,alarm_information中该设备所有的告警信息全部被删除。
在device中创建触发器:
///////////////////////////////////////////////////////////////////////////////////////////////
create trigger update_alarm_information before delete on device
for each row
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;
///////////////////////////////////////////////////////////////////////////////////////////////
如果用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 ;