为什么触发器SET仅适用于减号运算而不适用于总和?

时间:2018-10-13 16:45:37

标签: sql oracle plsql oracle11g

为什么如果我在 parking 表上执行INSERT查询,它会执行更新操作,而如果我执行DELETE查询,只会打印“ hi “ 消息,但不添加+1吗?

CREATE OR REPLACE TRIGGER employees

AFTER INSERT OR DELETE ON parking
FOR EACH ROW
BEGIN

IF INSERTING THEN
UPDATE area_p
SET Capacity = Capacity-1
WHERE NAME_AREA = :NEW.N_AREA;

IF DELETING THEN
DBMS_OUTPUT.PUT_LINE('hi');
UPDATE area_p
SET Capacity = Capacity+1
WHERE NAME_AREA = :NEW.N_AREA;
END IF;

END;
/

1 个答案:

答案 0 :(得分:2)

您需要为:OLD使用DELETE

CREATE OR REPLACE TRIGGER employees
AFTER INSERT OR DELETE ON parking
FOR EACH ROW
BEGIN
  IF INSERTING THEN
    UPDATE area_p
    SET Capacity = Capacity-1
    WHERE NAME_AREA = :NEW.N_AREA;
  END IF;

  IF DELETING THEN
    DBMS_OUTPUT.PUT_LINE('hi');     -- you should avoid print from trigger
    UPDATE area_p
    SET Capacity = Capacity+1
    WHERE NAME_AREA = :OLD.N_AREA;  -- here
  END IF;
END;
/

来自About OLD and NEW Pseudorecords

  

对于触发器正在处理的行:

     
      
  • 对于INSERT触发器,OLD不包含任何值,而NEW包含新的值。

  •   
  • 对于UPDATE触发器,OLD包含旧值,NEW包含新值。

  •   
  • 对于DELETE触发器,OLD包含旧值,而NEW则不包含值。

  •