title: Delete all rows in all tables which aren't used in any FK relation anymore
body: <p>In order to trim a production database for loading in a test system, we've deleted rows in many tables. This now left us with cruft in a couple of tables, namely rows which aren't used in any FK relation anymore. What I want to achieve is like the garbage collection in Java.</p>
  
  <p>Or to put it another way: If I have M tables in the database. N of them (i.e. most but not all) have foreign key relations. I've deleted a couple of high level rows (i.e. which only have outgoing FK relations) via SQL. This leaves the rows in the related tables alone.</p>
  
  <p>Does someone have a SQL stored procedure or a Java program which finds the N tables and then follows all the FK relations to delete rows which are no longer needed.</p>
  
  <p>If finding the N tables to too complex, I could probably provide the script a list of tables to scan or, preferably, a negative list of tables to ignore.</p>
  
  <p>Also note:</p>
  
  <ol>
  <li>We have some tables which are used in many (>50) FK relations, i.e. <code>A</code>, <code>B</code>, <code>C</code>, ... all use rows in <code>Z</code>.</li>
  <li>All FK relations use the technical PK column which is always a single column.</li>
  </ol>