码迷,mamicode.com
首页 > 数据库 > 详细

【mysql】mysql触发器使用示例

时间:2018-04-12 13:28:36      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:需要   after   dev   util   delete   sed   before   collate   一个   

mysql触发器

  • 时间点:before/after
  • 触发事件: update/delete/insert
  • 时间点+触发事件:构成一个完整的触发器的触发时机;
  • 一个触发时机最多只能由1个Trigger:如 before-insert最多只能有1个触发器,如果需要多个,需要在1个Trigger内些sql Statement;

old和new

  • insert:只有new关键字可以使用;
  • update: new和old关键字都可以使用;
  • delete: 只有old关键字可以使用;

完整语句

CREATE TABLE `capacity_pm` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘自增主键‘,
  `pool_id` char(36) CHARACTER SET utf8 DEFAULT NULL COMMENT ‘资源池ID‘,
  `cluster_lv1` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT ‘集群分类‘,
  `cluster_lv2` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT ‘集群2级分类‘,
  `update_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT ‘更新或创建时间‘,
  `templete_id` varchar(255) CHARACTER SET utf8 NOT NULL COMMENT ‘模板ID‘,
  `templete_name` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT ‘模板名称‘,
  `templete_cpu_core` int(10) unsigned zerofill NOT NULL COMMENT ‘模板CPU核数‘,
  `templete_mem_size` double NOT NULL COMMENT ‘模板内存大小‘,
  `templete_disk_size` double NOT NULL COMMENT ‘模板磁盘大小‘,
  `host_total` int(11) unsigned zerofill DEFAULT NULL COMMENT ‘主机总数‘,
  `host_used` int(11) unsigned zerofill DEFAULT NULL COMMENT ‘主机已分配数量‘,
  `cpu_core_total` int(11) unsigned zerofill DEFAULT NULL COMMENT ‘cpu总核数‘,
  `cpu_core_free` int(11) DEFAULT NULL,
  `cpu_core_used` int(11) DEFAULT NULL COMMENT ‘cpu已分配数量‘,
  `cpu_core_util` double(10,3) DEFAULT NULL COMMENT ‘cpu核数使用占比‘,
  `mem_total` double DEFAULT NULL COMMENT ‘内存总空间‘,
  `mem_free` double DEFAULT NULL,
  `mem_used` double DEFAULT NULL,
  `mem_util` double DEFAULT NULL COMMENT ‘内存使用占比‘,
  `disk_total` double DEFAULT NULL,
  `disk_free` double DEFAULT NULL,
  `disk_used` double DEFAULT NULL,
  `disk_util` double DEFAULT NULL COMMENT ‘磁盘使用占比‘,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_templete_all` (`pool_id`,`templete_id`) USING BTREE COMMENT ‘模块ID做完整索引‘
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TRIGGER `pm_before_insert_trigger` BEFORE INSERT ON `capacity_pm` FOR EACH ROW begin
set new.cpu_core_total=new.host_total * new.templete_cpu_core;
set new.cpu_core_used=new.host_used * new.templete_cpu_core;
set new.cpu_core_free=new.cpu_core_total - new.cpu_core_used;
set new.cpu_core_util=new.cpu_core_used / new.cpu_core_total;
end;

CREATE TRIGGER `pm_before_update_trigger` BEFORE UPDATE ON `capacity_pm` FOR EACH ROW begin
set new.cpu_core_total=new.host_total * old.templete_cpu_core;
set new.cpu_core_used=new.host_used * old.templete_cpu_core;
set new.cpu_core_free=new.cpu_core_total - new.cpu_core_used;
set new.cpu_core_util=new.cpu_core_used / new.cpu_core_total;
end;

创建触发器的语句

CREATE TRIGGER `pm_before_insert_trigger` BEFORE INSERT ON `capacity_pm` FOR EACH ROW begin
set new.cpu_core_total=new.host_total * new.templete_cpu_core;
set new.cpu_core_used=new.host_used * new.templete_cpu_core;
set new.cpu_core_free=new.cpu_core_total - new.cpu_core_used;
set new.cpu_core_util=new.cpu_core_used / new.cpu_core_total;
end;

CREATE TRIGGER `pm_before_update_trigger` BEFORE UPDATE ON `capacity_pm` FOR EACH ROW begin
set new.cpu_core_total=new.host_total * old.templete_cpu_core;
set new.cpu_core_used=new.host_used * old.templete_cpu_core;
set new.cpu_core_free=new.cpu_core_total - new.cpu_core_used;
set new.cpu_core_util=new.cpu_core_used / new.cpu_core_total;
end;

参考

mysql5.6-trigger官网

【mysql】mysql触发器使用示例

标签:需要   after   dev   util   delete   sed   before   collate   一个   

原文地址:https://www.cnblogs.com/ssslinppp/p/8806635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!