ecwork

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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;

posted on 2018-04-10 12:54  ecwork  阅读(166)  评论(0编辑  收藏  举报