需要触发器 php/mySQL 的帮助
我需要使用三个表使这个触发器工作。有人看到问题吗?
“数量”需要始终显示添加和拉取的最新数量。
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 | |
+--------------+--------------+-------+-------+
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 | |
+--------------+--------------+-------+-------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1- 不能使用
;
作为end
的最终分隔符。您需要在触发器之前设置分隔符。2-
after insert
触发器在逻辑上应具有前缀ai
,而不是upd
。3- 您无法在触发器所在的同一个表中更改
after
触发器中的值。因此,如果您(可能)需要更改cartons_added
中的值,则需要在before
触发器中执行此操作。4- 另一方面,您不能在
before
触发器中更改其他表中的值,因为这些更改可能会回滚,然后您的表就会不稳定,因此需要在触发后。
5-您可以在触发器中影响多个表,只需按照示例进行即可。
如果您想更改触发器自己的表中的某些值,请不要使用
update
,而是使用如下代码:1- You cannot use
;
as a final delimiter for theend
. You need to set a delimiter before the trigger.2- A
after insert
trigger should logically have a prefixai
, notupd
.3- You cannot change values in a
after
trigger in the same table the trigger is for. So if you (might) need to change values incartons_added
you need to do that in thebefore
trigger.4- On the other hand, you cannot change values in other tables in a
before
trigger, because these changes might rollback and then you have inconstancy i your tables, so that need to happen in theafter
trigger.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: