I want to create a trigger after there's an insert in 'order_items' table. The trigger is to minus the quantity of that product id in 'inventories' table by the quantity being insert to 'order_items' table. If the result of quantity after being subtracted is negative, then the quantity of 'inventories' will be changed back to the old quantity and the data in 'order_items' will be deleted (the insertion is failed). If the product id is in many warehouses, then just minus the quantity with the smallest warehouse_id
Example #1:
Product ID '101' is available in Warehouse ID '1','2','3'
So choose the product id '101' in warehouse id '1' (smallest)
Product ID '101' Quantity in Inventories = 10
Product ID '101' Quantity Order_Items = 15
So 10-15=-5 (negative)
So...
Product ID '101' Quantity in Inventories = 10 (back to old data)
Delete '101' datas from Order_Items
Example #2:
Product ID '102' Quantity in Inventories = 20
Product ID '102' Quantity Order_Items = 8
So 20-8=12
So...
Product ID '102' Quantity in Inventories = 12
This is my code so far, any ideas? Thanks!
CREATE OR REPLACE TRIGGER update_qty
AFTER INSERT
ON order_items
FOR EACH ROW
DECLARE
qty_diff number;
qty_in number;
total number;
BEGIN
select quantity,count(product_id) into qty_in,total
from inventories
where product_id=:new.product_id;
if(total>1) then
select product_id from inventories where warehouse_id=(select min(warehouse_id) from inventories);
else
qty_diff:=qty_in-:new.quantity;
if(qty_diff<0) then
i.quantity:=:old.quantity;
delete from order_items where product_id=:new.product_id;
else
update inventories
set quantity=qty_diff;
end if;
end if;
END;
/
BEGIN
insert into order_items(order_id,item_id,product_id,quantity,unit_price) values(12345,12345,12345,100,200);
END;
/
selectis doing acountbut is missing thegroup_id. The next query in yourif( total > 1)is missing theintoand is looking at the warehouse with the smallestwarehouse_idnot the warehouse with the smallestwarehouse_idthat has the product. You don't want todelete from order_items, you want to throw an exception. And if you're going toupdate inventoriesyou'd want to include awhereclause so that you're updating a single row.order_itemsand adjusts thequantityininventoriesrather than putting a bunch of logic in a trigger.