觸發器創建及Navicat中使用


  mysql中的觸發器(trigger)使用

 

  Trigger:

  示例:

mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account
    -> FOR EACH ROW SET @sum = @sum + NEW.amount;
Query OK, 0 rows affected (0.06 sec)

 

  

  解析:<原諒我這懶惰的搬運工>

   

   The CREATE TRIGGER statement creates a trigger named ins_sum that is associated with the account table. It also includes clauses that specify the trigger action time, the triggering event, and what to do when the trigger activates:

  • The keyword BEFORE indicates the trigger action time. In this case, the trigger activates before each row inserted into the table. The other permitted keyword here is AFTER.

  • The keyword INSERT indicates the trigger event; that is, the type of operation that activates the trigger. In the example, INSERT operations cause trigger activation. You can also create triggers for DELETE and UPDATE operations.

  • The statement following FOR EACH ROW defines the trigger body; that is, the statement to execute each time the trigger activates, which occurs once for each row affected by the triggering event. In the example, the trigger body is a simple SET that accumulates into a user variable the values inserted into the amount column. The statement refers to the column as NEW.amount which means the value of theamount column to be inserted into the new row.”

 

  具體參見:http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

 

 

 

 

 

 

 

  Navicat中使用

  1.選中要添加觸發器的表;

    2.打開其設計表;

    3.打開觸發器,在指定欄中設置觸發器;

   

 

   具體參見:http://blog.csdn.net/cqnuztq/article/details/9735245

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM