mysqlmysql触发器使用示例

Posted ssslinppp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysqlmysql触发器使用示例相关的知识,希望对你有一定的参考价值。

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官网

以上是关于mysqlmysql触发器使用示例的主要内容,如果未能解决你的问题,请参考以下文章

MySQLMySQL 中 WITH 子句详解:从基础到实战示例

我如何使用 codeanywhere 片段

需要示例代码片段帮助

片段创建的 Intent 不会触发 onNewIntent

更改页面javascript代码(TamperMonkey)以将键盘笔触发送到父DOM

导航到另一个片段时触发 API 调用