1

我目前是 Firebird 的新手,尤其是触发器。通常,我在脚本中手动执行此操作,但我真的很着迷于使用触发器创建它。

请让我先解释一下我的表。

***STOCK***
CODE
NAME
TOTAL
GOOD
BROKEN
SERVICE
***DETAIL***
ID
STOCK_CODE
SERIAL
***BROKEN***
DETAIL_ID
MARK
***SERVICE***
DETAIL_ID
START_DATE
END_DATE
COST
***LOGS***
DETAIL_ID
MARK
START_DATE
END_DATE
COST

现在我的问题:

  1. 将新记录插入 BROKEN 后如何修改 STOCK.GOOD 和 STOCK.BROKEN 值?那将是:STOCK.GOOD-1,STOCK.BROKEN+1。

  2. 在删除 SERVICE 中的当前记录之前,如何将 BROKEN 和 SERVICE 中的所有记录插入 LOGS?

我希望我的问题能被接受。

4

1 回答 1

1

下面是两个触发器:

CREATE TRIGGER bi_broken FOR broken
  BEFORE INSERT
  POSITION 0
AS
BEGIN
  UPDATE stock SET good = good - 1, broken = broken + 1
    WHERE code = (SELECT d.stock_code 
      FROM detail d WHERE d.id = NEW.detail_id);
END


CREATE TRIGGER bd_service FOR service
  BEFORE DELETE
  POSITION 0
AS
BEGIN
  INSERT INTO logs (detail_id, mark, start_date, end_date, cost)
  SELECT detail_id, (SELECT b.mark FROM broken b WHERE b.detail_id = OLD.detail_id),
    start_date, end_date, cost
  FROM service 
  WHERE detail_id = OLD.detail_id;
END

顺便说一句,将标记放在单独的表中的原因是什么?它属于 STOCK,不是吗?

于 2012-08-14T06:31:35.417 回答