Triggers — MySQL 5.6 and 5.7_MySQL

php中文网
发布: 2016-06-01 13:14:03
原创
1210人浏览过

mysql triggers are changing in 5.7 in a big way. triggers have been around since 5.0 and have not changed much up to 5.6 but will gain the ability to have multiple triggers on the same event. previously you had one trigger maximum on a before update, for example, and now you can have multiple triggersandset their order.

So what is a trigger? Triggers run eitherBEFOREorAFTERanUPDATE,DELETE, orINSERTis performed. You also get access to theOLD.col_nameandNEW.col_namevariables for the previous value and the newer value of the column.

So how do you use a trigger? Let say you are updating the price of an inventory item in a product database with a simple UPDATE statement. But you also want to track when the price change and the old price.

The table for products.
CREATE TABLE products (id INT NOT NULL auto_increment,<br> price DECIMAL(5,2) NOT NULL,<br> PRIMARY KEY (id));

The table for price changes on the product table.
CREATE TABLE products_log (id INT NOT NULL,<br> price DECIMAL(5,2) NOT NULL,<br> change_date timestamp);

Now to define a trigger that will log price changes. We do this when a price is updated. Now the use od OLD.price to avoid confusion between the old price or the new price being saved in the log.
DELIMITER |<br> CREATE TRIGGER product_price_logger<br> BEFORE UPDATE ON products<br> FOR EACH row<br> BEGIN<br> INSERT INTO products_log (id, price)<br> VALUES (id, OLD.PRICE);<br> END<br> |<br> DELIMITER ;

Add in some data.
INSERT INTO products (price) VALUES (1.10),(2.24),(.99),(.01),(.34);

So UPDATE a record.
UPDATE products SET price='1.11' WHERE ID = 1;

MacsMind
MacsMind

电商AI超级智能客服

MacsMind 192
查看详情 MacsMind

So did it work? Yes, and no. RunningSELECT * FROM products_log;Provides us with a time stamp of the change and the OLD.price. But I forgot to also record the id!!Challenge: Correct my mistake and compare it to an update I will make in a few days.

Now 5.7 introduces multiple triggers for the same event. Lets add yet another log this time recording who made the change;

The ‘who made the change table’.
CREATE table who_changed (<br> id INT NOT NULL,<br> who_did_it CHAR(30) NOT NULL,<br> when_did_it TIMESTAMP);

And the second trigger.
DELIMITER |<br> CREATE TRIGGER product_price_whom<br> BEFORE UPDATE ON products<br> FOR EACH ROW<br> FOLLOWS product_price_logger<br> BEGIN<br> INSERT INTO who_changed (id, who_did_it)<br> VALUES (OLD.id, user());<br> END<br> |<br> DELIMITER ;

SoUPDATE products SET price='19.99' WHERE id=4;is run and we see that both triggers execute. Note thatSHOW TRIGGERS fromschema;does not provide any information on trigger order. But you can find all that asaction_orderinPERFORMANCE_SCHEMA.TRIGGERS

Being able to order triggers makes it easy to make logical steps when processing data. Can you get into trouble with this? I am certain someone will manage to make a mess with this. But I think most of us will enjoy being able to use this great new functionality.

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号