title: Remove rows NOT referenced by a foreign key
body: <p>This is somewhat related to <a href="https://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys">this question</a>:</p>
  
  <p>I have a table with a primary key, and I have several tables that reference that primary key (using foreign keys).  I need to remove rows from that table, where the primary key isn't being referenced in any of those other tables (as well as a few other constraints).</p>
  
  <p>For example:</p>
  
  <pre><code>Group
  groupid | groupname
      1   |    'group 1'
      2   |    'group 3'
      3   |    'group 2'
      ... |    '...'
  
  Table1
  tableid | groupid | data
      1   |    3    |    ...
      ... |    ...  |    ...
  
  Table2
  tableid | groupid | data
      1   |    2    |    ...
      ... |    ...  |    ...
  </code></pre>
  
  <p>and so on.  Some of the rows in Group aren't referenced in any of the tables, and I need to remove those rows.  In addition to this, I need to know how to find all of the tables/rows that reference a given row in Group.</p>
  
  <p>I know that I can just query every table and check the groupid's, but since they are foreign keys, I imagine that there is a better way of doing it.</p>
  
  <p>This is using Postgresql 8.3 by the way.</p>