> 데이터 베이스 > MySQL 튜토리얼 > Triggers — MySQL 5.6 and 5.7_MySQL

Triggers — MySQL 5.6 and 5.7_MySQL

WBOY
풀어 주다: 2016-06-01 13:14:03
원래의
947명이 탐색했습니다.

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;

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.

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿