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;

  • 相关阅读:
    [爬虫资源]各大爬虫资源大汇总,做我们自己的awesome系列
    [Nancy On .Net Core Docker] 轻量级的web框架
    2015,平凡之路
    转[开发环境配置]在Ubuntu下配置舒服的Python开发环境
    转自coolshell--vim的基本操作
    [python基础]关于包,类,模块的那些事儿
    [python IDE] 舒服的pycharm设置
    [python基础]关于中文编码和解码那点事儿
    [python基础]关于装饰器
    小白也能看懂的插件化DroidPlugin原理(三)-- 如何拦截startActivity方法
  • 原文地址:https://www.cnblogs.com/ecwork/p/8778246.html
Copyright © 2011-2022 走看看