• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題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關鍵字專題關鍵字專題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

    來源:懂視網 責編:小采 時間: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.

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。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
    主站蜘蛛池模板: 欧美成人精品一区二区综合| 亚洲av无码精品网站| 欧美 日韩 精品 另类视频| 经典国产乱子伦精品视频| 久久精品国产一区二区三区不卡| 国产vA免费精品高清在线观看| 四虎国产精品永久地址49| 人妻精品久久无码专区精东影业| 国产专区日韩精品欧美色| 国产精品宾馆在线精品酒店| 亚洲欧美日韩国产成人精品影院| 国产99久久九九精品无码| 国产精品毛片VA一区二区三区| 国产成人精品无码一区二区| 国产精品自在欧美一区| 久久91精品国产91久久户| 国产精品99久久不卡| 亚洲一区爱区精品无码| 99精品久久久久久久婷婷| 亚洲国产精品无码av| 国产高清在线精品一区二区 | 精品人妻V?出轨中文字幕| 国产精品一区二区久久| 日韩精品无码免费视频| 久久精品中文字幕第23页| 国产精品手机在线观看你懂的| 国产在线拍揄自揄视精品不卡| 国产精品jizz视频| 久久久久99精品成人片直播| 一本色道久久88精品综合| 久久久人妻精品无码一区| 国产精品亚洲高清一区二区| 99在线精品免费视频| 国产高清在线精品一区二区| 成人精品视频成人影院| 国产精品成人小电影在线观看| 成人国产一区二区三区精品| 国产精品污WWW一区二区三区| 成人国内精品久久久久影院VR| 99久久人人爽亚洲精品美女| www亚洲欲色成人久久精品|