Home  >  Article  >  Database  >  [MySQL 14] Trigger after and before

[MySQL 14] Trigger after and before

黄舟
黄舟Original
2017-02-04 13:49:211375browse

after completes the addition, deletion and modification of data first, and then triggers. The triggered statement is later than the monitored addition, deletion and modification operation, and cannot affect the previous addition, deletion and modification operations; that is to say, the order record is inserted first, and then the quantity of the product is updated;

before is to complete the trigger first, and then add, delete or modify. The triggered statement precedes the monitored addition, deletion or modification, so we have the opportunity to judge and modify the upcoming operation;

Case:
Product g table The number of juzi is only 20, but if more than 20 are added to order o, how to solve it at this time

#创建触发器test4  
CREATE TRIGGER test4  
AFTER  
INSERT  
ON `ord`  
FOR EACH ROW  
BEGIN  
UPDATE goods SET num= num - new.much WHERE goods_id = new.gid;  
END$$

[MySQL 14] Trigger after and before

Trigger usage before:

Trigger first, then add, delete or modify after judgment and processing. The maximum quantity of the order is modified according to the inventory. Of course, I simply fixed a value. In fact, you can use statements to obtain dynamic inventory values.

#创建触发器test5  
CREATE TRIGGER test5  
BEFORE  
INSERT  
ON `ord`  
FOR EACH ROW  
BEGIN  
  IF new.much >26 THEN  
     SET new.much = 26;  
  END IF;  
UPDATE goods SET num= num - new.much WHERE goods_id = new.gid;  
END$$

[MySQL 14] Trigger after and before

The above is the content of [MySQL 14] trigger after and before. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn