【mysql】mysql觸發器使用示例
阿新 • • 發佈:2018-04-12
mysql5 大小 use 資源池 nod for HR pac nta
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觸發器使用示例