title: Update Trigger PL/SQL Oracle
body: <p>I have a table with the following structure: </p>
  
  <pre><code>create table treballa (
  code varchar2(4), 
   name varchar2(20), 
   director varchar2(4), 
  department number, 
  salary int,
   primary key (code), 
   foreign key (director) references treballa(code) 
  ) 
  </code></pre>
  
  <p>And I need to create 1 trigger that checks if the department number of the updated worker, the total salary between all the workers of that department doesn't exceeds 10000, but I don't know how to do it really... Could you please help me? Thank you very much.</p>
  
  <p>EDIT:</p>
  
  <pre><code>CREATE OR REPLACE TRIGGER controlsalaridept BEFORE INSERT OR UPDATE ON TREBALLA
  FOR EACH ROW
    DECLARE
      salaritotal INT := 0;
    BEGIN
      IF INSERTING THEN
        SELECT sum(salary) INTO salaritotal FROM TREBALLA WHERE DEPARTMENT LIKE :new.DEPARTAMENT;
        DBMS_OUTPUT.PUT_LINE('Salari Total abans suma:'||salaritotal);
        salaritotal := salaritotal + :new.SALARY;
        DBMS_OUTPUT.PUT_LINE('Salari Total després:'||salaritotal);
        IF salaritotal &gt; 10000 THEN
          raise_application_error(-20025, 'La suma del salari total de cada departament supera els 10000 Euros');
        END IF;
      END IF;
      IF UPDATING THEN
        SELECT sum(salary) INTO salaritotal FROM TREBALLA WHERE DEPARTMENT LIKE :old.DEPARTAMENT;
        DBMS_OUTPUT.PUT_LINE('Salari Total abans suma:'||salaritotal);
        salaritotal := salaritotal - :old.SALARY + :new.SALARY;
        DBMS_OUTPUT.PUT_LINE('Salari Total després:'||salaritotal);
        IF salaritotal &gt; 10000 THEN
          raise_application_error(-20026,'La suma del salari total de cada departament supera els 10000 Euros');
        END IF;
      END IF;
    END;
  </code></pre>
  
  <p>END EDIT/</p>
  
  <p>ERROR WHEN UPDATING:</p>
  
  <blockquote>
    <p>[42000][4091] ORA-04091: table SPECIAL.TREBALLA is mutating,
    trigger/function may not see it ORA-06512: at
    "SPECIAL.CONTROLSALARIDEPT", line 14 ORA-04088: error during execution
    of trigger 'SPECIAL.CONTROLSALARIDEPT'</p>
  </blockquote>
  
  <p>PD: Sorry, I'm very new on oracle and I need help with this trigger, I don't know even if it's right what I'm doing.... The first part of the trigger "IF INSERTING" works well, the problem is with the UPDATING...</p>