title: Deleting rows from parent and child tables
body: <p>Assume two tables in Oracle 10G</p>
  
  <pre><code>TableA (Parent) --&gt; TableB (Child)
  </code></pre>
  
  <p>Every row in TableA has several child rows related to it in TableB. </p>
  
  <p>I want to delete specific rows in TableA which means i have to delete the related rows in tableB first. </p>
  
  <p>This deletes the child entries</p>
  
  <pre><code>delete from tableB where last_update_Dtm = sysdate-30;
  </code></pre>
  
  <p>To delete the parent rows for the rows just deleted in the child table I could do something like this</p>
  
  <pre><code>Delete from TableA where not exists (select 1 from tableB where tableA.key=tableB.key);
  </code></pre>
  
  <p>The above will will also delete rows in the child table where (last_update_Dtm = sysdate-30) is false.  TableA does not have a last_update_dtm column so there is no way of knowing which rows to delete without the entries in the child table. </p>
  
  <p>I could save the keys in the child table prior to deleting but this seems like an expensive approach. What is the correct way of deleting the rows in both tables?</p>
  
  <h2>Edit</h2>
  
  <p>To explain better what i am trying to achieve, the following query would have done what i am trying to do if there was no constraint between the two table. </p>
  
  <pre><code>Delete from tableA
  Where exists (
  Select 1 from tableB
  where tableA.key=tableB.key
  and tableB.last_update_dtm=sysdate-30)
  
  Delete from tableB where last_update_dtm=systdate-30
  </code></pre>