`

mysql触发器

阅读更多
有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创建触发器就更加方便了

首先选择表device,点击右键,在弹出菜单中选择“设计表”
选择“触发器”


3.  在定义框中写入:

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.  完成


本文来自CSDN博客,转载请标明出处:file:///E:/工作/2009/200908/20090831/Mysql触发器实例%20-%20xxben的专栏%20-%20CSDN博客.htm
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics