mysql 5.1 trigger_MySQL 5.1触发器小试

本文介绍了MySQL 5.1中的触发器功能,通过一个实例展示了如何使用AFTER INSERT触发器将数据从一个表复制到另一个表。关键词OLD和NEW用于访问触发器影响的行,BEFORE和AFTER定义触发器执行时间。
摘要由CSDN通过智能技术生成

我一直以为MySQL 5.0的触发器好似没那么强大,被一个搞mssql的老大给忽悠了,其实MySQL触发器从5.0就变得很强大,当然包括5.1。这

我一直以为MySQL 5.0的触发器好似没那么强大,被一个搞mssql的老大给忽悠了,其实MySQL触发器从5.0就变得很强大,当然包括5.1。

这里拿一个demo说事:

DROP TRIGGER IF EXISTS `ucenterhome`.`uchome_doing_test`;

DELIMITER //

CREATE TRIGGER `ucenterhome`.`uchome_doing_test` AFTER INSERT ON `ucenterhome`.`uchome_doing`

FOR EACH ROW BEGIN

INSERT INTO uchome_doing_1 SET doid= NEW.doid+3, uid= NEW.uid , username= NEW.username,`from`=NEW.`from`,dateline=NEW.dateline,message=NEW.message,ip=NEW.ip,replynum = NEW.replynum,mood=NEW.mood;

END

//

DELIMITER ;

每向uchome_doing表中插一条数据,,就复制到uchome_dong_1表中一条。

有两个关键字:

OLD NEW

官方的解释:

The OLD and NEW keywords enable you to access columns in the rows affected by a trigger. (OLD and NEW are not case sensitive.) In an INSERT trigger, only NEW.col_name can be used; there is no old row. In a DELETE trigger, only OLD.col_name can be used; there is no new row. In an UPDATE trigger, you can use OLD.col_name to refer to the columns of a row before it is updated and NEW.col_name to refer to the columns of the row after it is updated.

还有两个关键字

BEFORE AFTER

官方解释

The keyword BEFORE indicates the trigger action time. In this case, the trigger should activate before each row inserted into the table. The other allowable keyword here is AFTER.

logo.gif

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值