-- 1) INSERT（Create）用トリガ関数
CREATE OR REPLACE FUNCTION "MYSCHEMA"."TRG_STAFF_CREATE_FN"()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
    INSERT INTO "MYSCHEMA"."STAFF_EVENT" ("OBJECT_KEY","OBJECT_VERB")
    VALUES (NEW."STAFFNUM", 'Create');
    RETURN NEW;
END;
$$;

-- 2) DELETE 用トリガ関数
CREATE OR REPLACE FUNCTION "MYSCHEMA"."TRG_STAFF_DELETE_FN"()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
    INSERT INTO "MYSCHEMA"."STAFF_EVENT" ("OBJECT_KEY","OBJECT_VERB")
    VALUES (OLD."STAFFNUM", 'Delete');
    RETURN OLD;
END;
$$;

-- 3) UPDATE 用トリガ関数
CREATE OR REPLACE FUNCTION "MYSCHEMA"."TRG_STAFF_UPDATE_FN"()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
    INSERT INTO "MYSCHEMA"."STAFF_EVENT" ("OBJECT_KEY","OBJECT_VERB")
    VALUES (NEW."STAFFNUM", 'Update');
    RETURN NEW;
END;
$$;

-- === TRIGGER 本体 ===
-- AFTER INSERT
CREATE TRIGGER "STAFF_CREATE"
AFTER INSERT ON "MYSCHEMA"."STAFF"
FOR EACH ROW
EXECUTE FUNCTION "MYSCHEMA"."TRG_STAFF_CREATE_FN"();

-- AFTER DELETE
CREATE TRIGGER "STAFF_DELETE"
AFTER DELETE ON "MYSCHEMA"."STAFF"
FOR EACH ROW
EXECUTE FUNCTION "MYSCHEMA"."TRG_STAFF_DELETE_FN"();

-- AFTER UPDATE
CREATE TRIGGER "STAFF_UPDATE"
AFTER UPDATE ON "MYSCHEMA"."STAFF"
FOR EACH ROW
EXECUTE FUNCTION "MYSCHEMA"."TRG_STAFF_UPDATE_FN"();