title: Oracle trigger: SELECT INTO, no data found
body: <p>I have the following table that describes which chemical elements each planet is composed of using percentage.</p>
  
  <pre><code>CREATE TABLE elem_in_planet
  (
      id_planet INTEGER,
      element_symbol CHAR(2),
      percent_representation NUMBER CONSTRAINT NN_elem_in_planet NOT NULL,
  
      CONSTRAINT PK_elem_in_planet PRIMARY KEY (id_planet, element_symbol),
      CONSTRAINT FK_planet_has_elem FOREIGN KEY (id_planet) REFERENCES planet (id_planet),
      CONSTRAINT FK_elem_in_planet FOREIGN KEY (element_symbol) REFERENCES chemical_element (element_symbol)
  );
  </code></pre>
  
  <p>I'm trying to make a trigger that warns users when they add a new element to a planet and the sum of elements in that planet exceeds 100%. I came up with this.</p>
  
  <pre><code>CREATE OR REPLACE TRIGGER elem_in_planet_check
      AFTER INSERT OR UPDATE ON elem_in_planet
      FOR EACH ROW
  DECLARE
      sum_var NUMBER;
      PRAGMA autonomous_transaction;
  BEGIN
      SELECT SUM(percent_representation)
      INTO sum_var
      FROM elem_in_planet
      WHERE id_planet = :NEW.id_planet
      GROUP BY id_planet;
  
      EXCEPTION
          WHEN NO_DATA_FOUND THEN
              sum_var := 0;
  
      IF sum_var &gt; 100 THEN
          DBMS_OUTPUT.put_line('WARNING: Blah blah.');
      END IF;
  END;
  /
  </code></pre>
  
  <p>This code seems to throw the NO_DATA_FOUND exception every single time, even though I have inserted test data and when I run the SQL query alone, it works as expected.</p>
  
  <p>I'm new to this and don't understand what I'm doing wrong.</p>
  
  <p>Thank you for any advice.</p>