我如何创建一个触发器来更新来自 postgresql 中另一个 table 的数据?

How can I create a trigger that updates data from another table in postgresql?

我有 2 tables“销售”和“产品”。 table“produts”有一个 id 列和一个 amount 列。 我想创建一个触发器,当我在“销售”中插入新销售时,它会从“产品”中的“数量”中删除 1。 我该怎么做?

我的触发函数:

CREATE OR REPLACE FUNCTION public.sell()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    VOLATILE
    COST 100
AS $BODY$
BEGIN
   NEW."amount" := product.amount - 1;
   RETURN NEW;
END
$BODY$;

此处 link 文档。

CREATE OR REPLACE TRIGGER  update_product_on_selling AFTER INSERT ON  sellings EXECUTE  public.sell();

这里是文档的摘录

CREATE [ OR REPLACE ] [ CONSTRAINT ] TRIGGER name { BEFORE | AFTER | INSTEAD OF } { event [ OR ... ] }
    ON table_name
    [ FROM referenced_table_name ]
    [ NOT DEFERRABLE | [ DEFERRABLE ] [ INITIALLY IMMEDIATE | INITIALLY DEFERRED ] ]
    [ REFERENCING { { OLD | NEW } TABLE [ AS ] transition_relation_name } [ ... ] ]
    [ FOR [ EACH ] { ROW | STATEMENT } ]
    [ WHEN ( condition ) ]
    EXECUTE { FUNCTION | PROCEDURE } function_name ( arguments )

where event can be one of:

    INSERT
    UPDATE [ OF column_name [, ... ] ]
    DELETE
    TRUNCATE