Your trigger appears to work fine, if there's already data in the table:
SQL> insert into highlineitem (item_id, quantity, description, captain_id) values (1, 1, 'Soccer Ball', 1);
1 row created.
SQL> insert into highlineitem (item_id, quantity, description, captain_id) values (2, 8, 'Soccer Ball', 1);
Your insert statement was revised to allow 6 Soccer Balls to be checked out.
1 row created.
The problem is if there isn't already any data in this table:
SQL> delete from highlineitem;
2 rows deleted.
SQL> insert into highlineitem (item_id, quantity, description, captain_id) values (3, 999, 'Soccer Ball', 1);
1 row created.
The reason this happens is because a SUM
of no data is NULL
, not zero. Arithmetic operations involving NULL
return NULL
, so your variable TOTAL
contains NULL
, and as NULL
is not greater than 7, the quantity doesn't get altered.
The fix is fairly simple: replace SUM(QUANTITY)
with NVL(SUM(QUANTITY), 0)
.