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;

  • 相关阅读:
    Windows Server 2008 R2 下配置证书服务器和HTTPS方式访问网站
    C# AD(Active Directory)域信息同步,组织单位、用户等信息查询
    Windows Server 2008 R2 配置Exchange 2010邮件服务器并使用EWS发送邮件
    体验vs11 Beta
    jQuery Gallery Plugin在Asp.Net中使用
    第一个Python程序——博客自动访问脚本
    网盘:不仅仅是存储
    TCP/UDP端口列表
    Linux的时间 HZ,Tick,Jiffies
    Intel Data Plane Development Kit(DPDK) 1.2.3特性介绍
  • 原文地址:https://www.cnblogs.com/ecwork/p/8778246.html
Copyright © 2011-2022 走看看