title: UPDATE one table upon INSERT INTO another table
body: <p>I have a database with two tables:</p>
  
  <ul>
  <li>devices</li>
  <li>temperature</li>
  </ul>
  
  <p>The schema follows:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS devices(
    device_id serial PRIMARY KEY,
    device_name varchar(255) UNIQUE NOT NULL,
    last_record_time timestamp without time zone DEFAULT '1995-10-30 10:30:00'
  );
  
  CREATE TABLE IF NOT EXISTS temperature(
    device_id integer NOT NULL,
    temperature decimal NOT NULL,
    record_time timestamp without time zone NOT NULL,
    CONSTRAINT temperature_device_id_fkey FOREIGN KEY (device_id)
      REFERENCES devices (device_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
  );
  </code></pre>
  
  <p>The devices table keeps a list of all the devices. So there is a unique id for each device. The temperature table aggregates data from all of the devices. You can select by device_id to see all the entries that pertain to a specific device.</p>
  
  <p>I have the constraint that I cannot delete from the devices table because the temperature table depends on it. I would also like the devices table to be updated when a new record is inserted into the temperature table. </p>
  
  <p>That is, the record_time from a new record in the temperature should become the last_record_time for that device's entry in the devices table. That way I always know when was the last time a device inserted data. </p>
  
  <p>I am currently doing this programmatically. I insert records, and immediate select them right back out and write into the other table. This is introducing some bugs. So, I would prefer to automate this at the database level. How can I go about resolving this?</p>