【mysql】mysql觸發器使用樣本

來源:互聯網
上載者:User

標籤:需要   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 beginset 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 beginset 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 beginset 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 beginset 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觸發器使用樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.