博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
50.9. 触发器(Trigger)
阅读量:5973 次
发布时间:2019-06-19

本文共 3830 字,大约阅读时间需要 12 分钟。

50.9.1. create trigger

50.9.1.1. Update 更新出发

实现 history 历史表功能,BEFORE update 做到数据库更新自动备份

CREATE TABLE user_history SELECT * FROM user WHERE 1 <> 1DELIMITER //CREATE TRIGGER user_history BEFORE update ON user FOR EACH ROWBEGINinsert into user_history SELECT * FROM user WHERE id = OLD.id;END; //DELIMITER ;

判断某字段数据修改满足条件后出发。

CREATE DEFINER=`dba`@`%` TRIGGER `cms`.`jc_content_BEFORE_UPDATE` BEFORE UPDATE ON `jc_content` FOR EACH ROWBEGIN	IF NEW.status = '1' THEN		insert into `neo`.elasticsearch_trash(id) values(OLD.content_id);	END IF;    IF NEW.status = '2' THEN		delete from `neo`.elasticsearch_trash where id = OLD.content_id;	END IF;END

50.9.1.2. Delete 删除出发

CREATE DEFINER=`dba`@`%` TRIGGER `cms`.`jc_content_BEFORE_DELETE` BEFORE DELETE ON `jc_content` FOR EACH ROWBEGIN	insert into `neo`.elasticsearch_trash(id) values(OLD.content_id);END

50.9.1.3. Insert 插入出发

 

50.9.2. drop trigger

DROP TRIGGER admin_user_history;DELIMITER //CREATE TRIGGER admin_user_history BEFORE update ON admin_user FOR EACH ROWBEGINinsert into admin_user_history SELECT * FROM admin_user WHERE user_id = OLD.user_id;END; //DELIMITER;

50.9.3. show triggers

show triggers;

50.9.3.1. SHOW CREATE TRIGGER

mysql> SHOW CREATE TRIGGER ins_sum\G*************************** 1. row ***************************               Trigger: ins_sum              sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTIONSQL Original Statement: CREATE DEFINER=`me`@`localhost` TRIGGER ins_sum                        BEFORE INSERT ON account                        FOR EACH ROW SET @sum = @sum + NEW.amount  character_set_client: utf8  collation_connection: utf8_general_ci    Database Collation: latin1_swedish_ci               Created: 2013-07-09 10:39:34.96

50.9.4. EXAMPLE

50.9.4.1. BEFORE/AFTER

例 50.1. BEFORE/AFTER

DROP TRIGGER MY_TEST_MONITOR;DELIMITER //CREATE TRIGGER MY_TEST_MONITOR BEFORE insert ON MY_TEST FOR EACH ROWBEGIN	INSERT INTO MY_TEST_MONITOR SELECT * FROM MY_TEST WHERE TICKET = NEW.TICKET;END; //DELIMITER;
DROP TRIGGER MY_TEST_MONITOR;DELIMITER //CREATE TRIGGER MY_TEST_MONITOR AFTER insert ON MY_TEST FOR EACH ROWBEGIN	INSERT INTO MY_TEST_MONITOR SELECT * FROM MY_TEST WHERE TICKET = NEW.TICKET;END; //DELIMITER;

通过触发器保护数据,防止重复插入数据

CREATE DEFINER=`neo`@`%` TRIGGER `members_before_insert` BEFORE INSERT ON `members` FOR EACH ROW BEGIN	IF new.username IS NOT NULL THEN		IF exists(select m.username from members m where m.username = new.username) THEN	   	set new.username = '';		END IF;	END IF;END

50.9.4.2. UUID

例 50.2. uuid()

delimiter $$CREATE TABLE `member` (  `uuid` char(36) NOT NULL,  `username` varchar(20) DEFAULT NULL,  `password` varchar(32) DEFAULT NULL,  PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8$$CREATEDEFINER=`root`@`%`TRIGGER `test`.`member_before_insert`BEFORE INSERT ON `test`.`member`FOR EACH ROWSET new.uuid = uuid()$$

50.9.4.3. CALL PROCEDURE

CREATE DEFINER=`neo`@`%` TRIGGER `accounts_angelfund` AFTER INSERT ON `accounts` FOR EACH ROW BEGIN	   IF new.paymode = 'angelfund' THEN		call angelfund(new.name,new.ctime);		   END IF;ENDCREATE DEFINER=`neo`@`%` PROCEDURE `angelfund`(IN `puid` VARCHAR(50), IN `ptime` DATETIME)	LANGUAGE SQL	NOT DETERMINISTIC	CONTAINS SQL	SQL SECURITY DEFINER	COMMENT ''BEGIN	DECLARE fusername VARCHAR(16) DEFAULT NULL;	DECLARE fchinese_name VARCHAR(16) DEFAULT NULL;	DECLARE fmembers_date VARCHAR(20) DEFAULT NULL;	SELECT username,chinese_name,FROM_UNIXTIME(createtime) INTO fusername,fchinese_name,fmembers_date FROM members WHERE username = puid;	IF fusername IS NOT NULL THEN		INSERT IGNORE INTO angelfund(username,chinese_name,members_date,accounts_date,endtime,`status`,op,operator,`description`) value(fusername,fchinese_name,fmembers_date,ptime,DATE_ADD(ptime, INTERVAL +1 MONTH),'N','N','computer','');   END IF;END

原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
Python中使用ElementTree解析xml
查看>>
linux的日志服务器关于屏蔽一些关键字的方法
查看>>
mysql多实例实例化数据库
查看>>
javascript 操作DOM元素样式
查看>>
HBase 笔记3
查看>>
【Linux】Linux 在线安装yum
查看>>
Atom 编辑器系列视频课程
查看>>
[原][osgearth]osgearthviewer读取earth文件,代码解析(earth文件读取的一帧)
查看>>
mybatis update返回值的意义
查看>>
expdp 详解及实例
查看>>
通过IP判断登录地址
查看>>
深入浅出JavaScript (五) 详解Document.write()方法
查看>>
Beta冲刺——day6
查看>>
在一个程序中调用另一个程序并且传输数据到选择屏幕执行这个程序
查看>>
代码生成工具Database2Sharp中增加视图的代码生成以及主从表界面生成功能
查看>>
关于在VS2005中编写DLL遇到 C4251 警告的解决办法
查看>>
提高信息安全意识对网络勒索病毒说不
查看>>
maya pyside 多个窗口实例 报错 解决
查看>>
我的友情链接
查看>>
IDE---Python IDE之Eric5在window下的安装
查看>>