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;

  • 相关阅读:
    程序员第一定律:关于技能与收入
    JS注册/移除事件处理程序
    关于程序猿,你不知道的15件事
    .NET后台输出js脚本的方法
    新项目经理必读:分析什么是项目经理
    项目如何开始:怎样和客户一起搞定需求
    【转】为什么程序员讨厌修改静态检查问题
    js的with语句使用方法
    软件版本号 详解
    PHP记忆碎片2投票汇总
  • 原文地址:https://www.cnblogs.com/ecwork/p/8778246.html
Copyright © 2011-2022 走看看