我需要在插入或删除员工(EMPLOYERS TABLE)时触发,表SHOPS UPDATE中的属性EMPTOTAL。
EMPLOYERS表有一个名为SHOP的字段(和外键),它引用了SHOPS表。
我知道它应该与此类似,但我没有任何涉及我的练习中超过1个表的示例。
CREATE OR REPLACE TRIGGER UPD_EMPTOTAL BEFORE INSERT OR DELETE ON EMPLOYERS FOR EACH ROW
DECLARE
BEGIN
IF INSERTING THEN
UPDATE SHOPS SET EMPTOTAL=EMPTOTAL+1;
ELSIF DELETING THEN
UPDATE SHOPS SET EMPTOTAL=EMPTOTAL-1;
END IF;
END;
(我已尝试过UPDATE句子或为商店声明变量等其他内容,但我不清楚这一点,所以我只是在这里解析了我最确定的代码)。
答案 0 :(得分:0)
这可能是让触发器工作所需的全部内容:
create or replace trigger upd_emptotal
before insert or delete on employers
for each row
declare
begin
if inserting
then
-- Update only shop total for the shop
-- in the employer record.
-- :new.shop is the value being inserted in the table.
update shops
set emptotal = emptotal + 1
where shop = :new.shop;
elsif deleting
then
-- Update only shop total for the shop
-- in the employer record.
-- :old.shop is the value in the record being deleted.
update shops
set emptotal = emptotal - 1
where shop = :old.shop;
end if;
end;