Changes between Initial Version and Version 1 of Trigger2


Ignore:
Timestamp:
08/21/26 07:38:49 (27 hours ago)
Author:
235018
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Trigger2

    v1 v1  
     1= Automatic update of product availability after an order
     2
     3=== Description
     4This trigger automatically decreases the available quantity of a product when the product is included in a customer order.
     5
     6The sells.quantity attribute represents the quantity of a product currently available in a store. When an order is placed, the quantity ordered must be deducted from the available quantity.
     7
     8This ensures that product availability is automatically synchronized with customer orders.
     9
     10==== Tables involved
     11- Product
     12- Order
     13- includes
     14- sells
     15
     16==== Type of trigger
     17- AFTER INSERT
     18- AFTER UPDATE
     19Activated on the `includes` table.
     20
     21==== SQL Code
     22{{{#!sql
     23CREATE OR REPLACE FUNCTION update_product_availability()
     24RETURNS TRIGGER
     25LANGUAGE plpgsql
     26AS $$
     27BEGIN
     28    -- When a product is newly added to an order,
     29    -- decrease the available quantity.
     30    IF TG_OP = 'INSERT' THEN
     31
     32        UPDATE sells
     33        SET quantity = quantity - (
     34            SELECT o.quantity
     35            FROM "order" o
     36            WHERE o.order_num = NEW.order_num
     37        )
     38        WHERE code = NEW.code;
     39
     40    -- If the product/order entry is modified,
     41    -- restore the old quantity and subtract the new quantity.
     42    ELSIF TG_OP = 'UPDATE' THEN
     43
     44        UPDATE sells
     45        SET quantity = quantity
     46            + (
     47                SELECT o.quantity
     48                FROM "order" o
     49                WHERE o.order_num = OLD.order_num
     50            )
     51            - (
     52                SELECT o.quantity
     53                FROM "order" o
     54                WHERE o.order_num = NEW.order_num
     55            )
     56        WHERE code = NEW.code;
     57
     58    END IF;
     59
     60    RETURN NULL;
     61END;
     62$$;
     63
     64
     65CREATE TRIGGER trg_update_product_availability
     66AFTER INSERT OR UPDATE
     67ON includes
     68FOR EACH ROW
     69EXECUTE FUNCTION update_product_availability();
     70
     71
     72}}}
     73
     74=== Logic explanation
     75
     76When a product is added to an order, the trigger:
     77
     78**1.** Identifies the ordered product using NEW.code.
     79**2.** Identifies the order using NEW.order_num.
     80**3.** Retrieves the ordered quantity from ORDER.
     81**4.** Finds the corresponding product in sells.
     82**5.** Decreases the available quantity by the ordered amount.
     83
     84For an update, the trigger compensates for the old quantity before applying the new quantity.
     85
     86For example:
     87
     88{{{#!TXT
     89
     90Initial product quantity:       20
     91Customer orders:                 3
     92
     93New available quantity:         20 - 3 = 17
     94
     95}}}
     96
     97=== Reason for trigger
     98
     99This trigger is useful because:
     100
     101- Product availability must automatically reflect customer orders.
     102- It prevents the application from having to manually update inventory.
     103- It reduces the possibility of inconsistent inventory data.
     104- It centralizes inventory-related business logic inside the database.
     105- It ensures that every order affects product availability consistently.