mysql 触发器使用场景_mysql三个应用场景

//创建表

DROP TABLE IF EXISTS updatelog;CREATE TABLE `updatelog` (

`id` int(11) NOT NULL AUTO_INCREMENT,`resourceid` int(11) DEFAULT NULL,`log` text,`createtime` datetime DEFAULT NULL,PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1

//必须指定主键或unique,不然无法replace

DROP TABLE IF EXISTS reslastlog;CREATE TABLE `reslastlog` (

`resourceid` int(11) NOT NULL DEFAULT '0',`log` text,`updatetime` datetime DEFAULT NULL,PRIMARY KEY (`resourceid`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1

//创建触发器

DROP TRIGGER IF EXISTS t_afterinsert_on_updatelog;delimiter //

CREATE TRIGGER t_afterinsert_on_updatelog

AFTER INSERT ON updatelog

FOR EACH ROW

BEGIN

replace into reslastlog(resourceid,log,updatetime) values(new.resourceid, new.log, new.createtime);END;//

delimiter;

DROP TRIGGER IF EXISTS t_afterdelete_on_updatelog;delimiter //

CREATE TRIGGER t_afterdelete_on_updatelog

AFTER DELETE ON updatelog

FOR EACH ROW

BEGIN

delete from reslastlog where resourceid=old.resourceid;END;//

delimiter;

//测试

insert into updatelog(resourceid, log, createtime) values(1, "version 1-0",now());insert into updatelog(resourceid, log, createtime) values(1, "version 1-1",now());insert into updatelog(resourceid, log, createtime) values(2, "version 2-2",now());delete from updatelog where resourceid = 2;//触发器相关操作

mysql> show triggers;+----------------------------+--------+-----------+--------------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+

| Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation |

+----------------------------+--------+-----------+--------------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+

| t_afterinsert_on_updatelog | INSERT | updatelog | BEGIN

replace into reslastlog(resourceid,log,updatetime) values(new.resourceid, new.log, new.createtime);END | AFTER | NULL | | root@localhost | latin1 | latin1_swedish_ci | latin1_swedish_ci |

| t_afterdelete_on_updatelog | DELETE | updatelog | BEGIN

delete from reslastlog where resouceid=old.resourceid;END | AFTER | NULL | | root@localhost | latin1 | latin1_swedish_ci | latin1_swedish_ci |

+----------------------------+--------+-----------+--------------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+2 rows in set (0.00sec)

drop trigger t_afterinsert_on_updatelog;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值