Creating an Updating trigger I have to create a trigger that calculate the the quantity in stock and this is my first one on PostgreSQL.

When entering the quantity bought at a Provider (Fournisseur) or a delivery at a client, the quantity has to be updated.

I tried:

CREATE OR REPLACE TRIGGER CalculQStockProduit(String)
    WHEN UPDATE QuantiteEnStock ON Produit
         BEGIN QuantiteEnStock = QuantiteEnStock-Quantite
         OR    QuantiteEnStock = QuantiteEnStock+Quantite


I know this wrong, not enough accurate. Can you help me learning how to create an accurate and right trigger?

The related database scheme in UML is (in French, unfortunately):
In PostgreSQL you have to create a trigger function that will perform the calculations and the use it in a trigger.

Example:

Create or replace function CalculQStockProduit() returns trigger as $$
begin;
 update QuantiteEnStock set NEW.QuantiteEnStock = NEW.QuantiteEnStock-NEW.Quantite;
return NEW;
end;
$$ language 'plpgsql';


Create trigger trgCalculQStockProduit after update on Produit
for each row execute procedure calc();


I would refer to trigger function in postgresql manual for better understanding

This is just a sample so you will have to work out the calculations needed.

PG Trigger functions

Choose according to your postgres version.

I have used plpgsql as language but there are various options. plpgsql, sql, plv8 etc etc