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$$
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$$
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)!