zoukankan      html  css  js  c++  java
  • Mysql Programming CS 155P笔记(五) Triggers

    Now that we understand functions and procedures and how to use them, it's a good time to talk about Triggers.

    A trigger is a set of sql statements which execute when a particular event occurs on a table.

    In MySQL, there are 6 events upon which you can associate a trigger:

    • BEFORE INSERT
    • AFTER INSERT
    • BEFORE UPDATE
    • AFTER UPDATE
    • BEFORE DELETE
    • AFTER DELETE

    They are useful for several reasons, but primarily they're written to insure data integrity.  If, for example, you want to make sure that age is never less than zero when we do an insert, we could create the following trigger:

    mysql> delimiter //
    mysql> CREATE TRIGGER agecheck BEFORE INSERT ON people FOR EACH ROW IF NEW.age < 0 THEN SET NEW.age = 0; END IF;//
    Query OK, 0 rows affected (0.00 sec)
    mysql> delimiter ;
    

    In this example, the "agecheck" trigger will run BEFORE data is inserted in the table.   We check to see if age is <0 and if it is, we insert a 0 instead.

    We could (and should) have set the column type to an unsigned datatype which would guarantee we couldn't insert a negative number, but without the trigger, mysql would generate an error when trying to insert the row.  We may not want that if we can just assume negative ages are 0.   So we gracefully insert the row and move on with our lives.

    One disadvantage of this approach is that the users of the table don't necessarily know that their inserted data was manipulated.  

    While triggers are associated with a specific table and actions on that table, they can run just about any sql query and can affect any other tables in that mysql database.   For example, we can have triggers on our "transaction_history" table, after insert, after delete and after update which update a "balance" table when a transaction is added or altered.    If we're keeping track of bank deposits and withdrawls, each transaction could add the transaction amount to the balance:

    CREATE TRIGGER `update_balance_after_update`
        AFTER UPDATE ON `transaction_history` FOR EACH ROW
        BEGIN
            UPDATE `balance` SET `balance`.`balance`=(`balance`.`balance`+`transaction_amount`-OLD.transaction_amount)  WHERE `balance`.`user_id` = OLD.user_id;
        END
    CREATE TRIGGER `update_balance_after_insert`
        AFTER INSERT ON `transaction_history` FOR EACH ROW
        BEGIN
            UPDATE `balance` SET `balance`.`balance`=(`balance`.`balance`+NEW.transaction_amount)  WHERE `balance`.`user_id` = NEW.user_id;
        END

    These examples should work if the transaction_amount is positive or negative.  The advantage of this approach is that we have a place to store the balance within the database and our application doesn't have to do the additional query to update the balance.   If the balance isn't directly manipulated, it should always be accurate.  When we want to get a current account balance for a user, we can just do a quick and simple query in the balance table instead of finding a SUM(`transaction_amount) WHERE user_id=...,  which is a slow operation.

    In the module's "resourses" section, are links to a tutsplus and w3recources pages which further describe triggers with examples.   Thoroughly read and understand both of them.

    create trigger 名称 时机(before/after) 事件(insert/update/delete)
    on 表格名称(指定trigger租用的表格) for each row 叙述(trigger执行的工作)
    begin
      .....
    end;

  • 相关阅读:
    单独编译和使用webrtc音频回声消除模块(附完整源码+测试音频文件)(转)
    Android手机直播(三)声音采集
    Android 音视频去回声、降噪(Android音频采集及回音消除)(转)
    Android中Selector的setSelected“方法不管用”
    Github的快捷键
    哔哩哔哩视频显示在Github的Markdown博客页方法
    小巧的屏幕录像软件oCam
    win7各种插件的下载与使用
    3D 打印机选择说明文档
    在Ubuntu 18.04中安装Pycharm及创建Pycharm快捷方式
  • 原文地址:https://www.cnblogs.com/ecwork/p/8778246.html
Copyright © 2011-2022 走看看