I'll try to explain my problem, linked to Trigger functionalities. I want to add a row in a table, focusing our attention on a specific number in a column, called "VOTO" (VOTE), for example:
ID UTENTE_ID DATA COMMENTO ISBN VOTO
50 423573 29-LUG-14 CAPOLAVORO! 9788804508359 5
After this inserting, it will be activated a Trigger like this:
create or replace TRIGGER CREATE_COMMENT
AFTER INSERT OR UPDATE ON RECENSIONI
FOR EACH ROW
pragma autonomous_transaction;
BEGIN
UPDATE LIBRI
SET VOTOPROD_MEDIA=(SELECT AVG(VOTO) FROM RECENSIONI WHERE ISBN=:NEW.ISBN GROUP BY ISBN)
WHERE ISBN=:NEW.ISBN;
END;
The idea behind this trigger is explained here: I had to add a row with a VOTE, then, it will be selected a row into another table called LIBRI (books) with a parameter called VOTOPROD_MEDIA (Average Rate for a book) where will be stored the Average Rate (AVG) of all votes of the comments related to that book.
EXAMPLE BEFORE TRIGGER:
TABLE COMMENTS:
ID USER_ID DATE COMMENT ISBN VOTE
50 423573 29-LUG-14 CAPOLAVORO! 9788804508359 5
TABLE BOOKS:
ISBN AVERAGE_VOTE
9788804508359 5
EXAMPLE OF WHAT I WOULD DO AFTER A TRIGGER:
TABLE COMMENTS:
ID USER_ID DATE COMMENT ISBN VOTE
50 423573 29-LUG-14 CAPOLAVORO! 9788804508359 5
51 423574 29-LUG-14 BAD BOOK! 9788804508359 1
TABLE BOOKS:
ISBN AVERAGE_VOTE
9788804508359 3
3 is the "AVG" value of 5 and 1.
Unfortunately, it doesn't work. The last inserting seems to do not affect that update. In fact, while the execution of this trigger the table COMMENTS, I suppose, is not yet updated with the new row inserted, so, when it tries to make the AVG, the VOTE in Table Books remains to 5, and not changes to 3.
Even though I use an "AFTER INSERT" I think that during the Trigger the table "RECENSIONI" remains in this state:
ID USER_ID DATE COMMENT ISBN VOTE
50 423573 29-LUG-14 CAPOLAVORO! 9788804508359 5
I'm almost sure about it because the same "problem" occurs in another trigger. Well, I wish to "avoid" this structural problem in an automatic way, creating a "way" to make this calculation after the insert or the update, but REALLY inserting that row.
How can I do this?