国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

Triggers—MySQL5.6and5.7_MySQL

來源:懂視網(wǎng) 責編:小采 時間:2020-11-09 19:17:42
文檔

Triggers—MySQL5.6and5.7_MySQL

Triggers—MySQL5.6and5.7_MySQL: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 BEFO
推薦度:
導讀Triggers—MySQL5.6and5.7_MySQL: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 BEFO

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,
price DECIMAL(5,2) NOT NULL,
PRIMARY KEY (id));

The table for price changes on the product table.
CREATE TABLE products_log (id INT NOT NULL,
price DECIMAL(5,2) NOT NULL,
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 |
CREATE TRIGGER product_price_logger
BEFORE UPDATE ON products
FOR EACH row
BEGIN
INSERT INTO products_log (id, price)
VALUES (id, OLD.PRICE);
END
|
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 (
id INT NOT NULL,
who_did_it CHAR(30) NOT NULL,
when_did_it TIMESTAMP);

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

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Triggers—MySQL5.6and5.7_MySQL

Triggers—MySQL5.6and5.7_MySQL: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 BEFO
推薦度:
標簽: and mysql mysql5
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 黑丝白浆| 久久精品国产一区二区三区 | 国产精品自在欧美一区 | 亚洲第一页色 | 国产毛片在线看 | 久久性色 | 欧美在线不卡视频 | 免费黄a | 亚洲国产一成人久久精品 | 日韩经典一区 | 可以看的毛片网站 | 99国产精品久久久久久久成人热 | 91精品欧美一区二区三区 | 91福利国产在线观一区二区 | 成人欧美一区二区三区在线 | 黄视频网站在线看 | 国产一区视频在线 | 国产欧美高清 | 成人精品一区二区www | 国产精品高清一区二区 | 精品国内自产拍在线视频 | 久久精品免费 | 一区二区三区四区亚洲 | 欧美天天干 | 99精品久久99久久久久 | 在线一区观看 | 97一区二区三区四区久久 | 成人精品在线视频 | 国产精品夜夜爽范冰冰 | 欧美中出 | 亚洲一区中文 | 国语对白91 | 久久久网久久久久合久久久久 | 日本大黄在线观看 | 日本三级韩国三级欧美三级 | 亚洲综合日韩在线亚洲欧美专区 | 国产一区二区精品久久 | 视频一区久久 | 俄罗斯女人禽交zozo | 婷婷久久五月天 | 欧美1页|