I need to make this trigger work using three tables. Does anyone see a problem?
The ‘qty’ needs to always show the latest quantity from the adds and pulls.
CREATE TRIGGER Upd_Cartons_Qty
AFTER INSERT ON cartons_added FOR EACH ROW
BEGIN
UPDATE cartons_current SET qty = qty + NEW.add_qty WHERE part_no = NEW.part_no;
END;
TABLE NAME: cartons_current
+--------------+--------------+-------+-------+
| Column | Type | Null | Key |
+--------------+--------------+-------+-------+
| part_no | varchar(20) | No | Prim |
| qty | int(8) | No | |
+--------------+--------------+-------+-------+
TABLE NAME: cartons-added
+--------------+--------------+-------+-------+
| Column | Type | Null | Key |
+--------------+--------------+-------+-------+
| part_no | varchar(20) | No | Prim |
| add_qty | int(8) | No | |
+--------------+--------------+-------+-------+
TABLE NAME: cartons_pulled
+--------------+--------------+-------+-------+
| Column | Type | Null | Key |
+--------------+--------------+-------+-------+
| part_no | varchar(20) | No | Prim |
| pull_qty | int(8) | No | |
+--------------+--------------+-------+-------+
1- You cannot use
;as a final delimiter for theend. You need to set a delimiter before the trigger.2- A
after inserttrigger should logically have a prefixai, notupd.3- You cannot change values in a
aftertrigger in the same table the trigger is for. So if you (might) need to change values incartons_addedyou need to do that in thebeforetrigger.4- On the other hand, you cannot change values in other tables in a
beforetrigger, because these changes might rollback and then you have inconstancy i your tables, so that need to happen in theaftertrigger.5- You can effect multiple tables in a trigger, just do it like the example.
If you want to alter some value in the triggers own table, don’t use
update, use code like below instead: