需要触发器 php/mySQL 的帮助

发布于 2024-12-06 18:13:57 字数 1208 浏览 1 评论 0原文

我需要使用三个表使这个触发器工作。有人看到问题吗?

“数量”需要始终显示添加和拉取的最新数量。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2024-12-13 18:13:57

1- 不能使用 ; 作为 end 的最终分隔符。您需要在触发器之前设置分隔符。
2- after insert 触发器在逻辑上应具有前缀 ai,而不是 upd
3- 您无法在触发器所在的同一个表中更改 after 触发器中的值。因此,如果您(可能)需要更改 cartons_added 中的值,则需要在 before 触发器中执行此操作。
4- 另一方面,您不能在 before 触发器中更改其他表中的值,因为这些更改可能会回滚,然后您的表就会不稳定,因此需要在 触发后。
5-您可以在触发器中影响多个表,只需按照示例进行即可。

DELIMITER $

CREATE TRIGGER ai_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;
  UPDATE cartons_pulled SET x1 = x1 + NEW.add_qty WHERE part_no = NEW.part_no;
END$

DELIMITER ;

如果您想更改触发器自己的表中的某些值,请不要使用 update,而是使用如下代码:

DELIMITER $

CREATE TRIGGER ai_Cartons_Qty BEFORE INSERT ON cartons_added FOR EACH ROW
BEGIN
  -- Update cartons_added .... will not work.
  -- Use SET NEW.fieldname instead.
  IF NEW.qty_added = 0 THEN 
    SET NEW.qty_added = 1;
  END IF;
END$

DELIMITER ;

1- You cannot use ; as a final delimiter for the end. You need to set a delimiter before the trigger.
2- A after insert trigger should logically have a prefix ai, not upd.
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 in cartons_added you need to do that in the before 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 the after trigger.
5- You can effect multiple tables in a trigger, just do it like the example.

DELIMITER $

CREATE TRIGGER ai_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;
  UPDATE cartons_pulled SET x1 = x1 + NEW.add_qty WHERE part_no = NEW.part_no;
END$

DELIMITER ;

If you want to alter some value in the triggers own table, don't use update, use code like below instead:

DELIMITER $

CREATE TRIGGER ai_Cartons_Qty BEFORE INSERT ON cartons_added FOR EACH ROW
BEGIN
  -- Update cartons_added .... will not work.
  -- Use SET NEW.fieldname instead.
  IF NEW.qty_added = 0 THEN 
    SET NEW.qty_added = 1;
  END IF;
END$

DELIMITER ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文