title: Deleting records from a table with heirachial data
body: <p>I have two tables in an SQL database with fields as follows:</p>
  
  <pre><code>Table A: Key | Id1 | Name
  
  Table B: Key | Id2 | Idx1 | Idx2 | Id1
  </code></pre>
  
  <p>Table B stores hierarchical data.  Field Id1 in Table B is linked back to Id1 in Table A with FKs set up such that deleting an item in Table 1 (say with Id1=1) will delete all of the items in Table B (with the same Id1) (ie. cascade).</p>
  
  <p>What I want to do is that for a record in Table B that is deleted with a certain Id1 (as a consequence of a record with the same Id1 in Table A), all of the records in Table B with the same Id2 to be deleted too.</p>
  
  <p>For example ...</p>
  
  <pre><code>Table A:
    1,1,A
    2,2,B
    3,3,C
  
  Table B:
    1,1,0,0,1
    2,1,0,1,2
    3,1,0,2,3
    4,1,1,0,3
    5,1,1,1,2
    6,2,0,0,3
  </code></pre>
  
  <p>I now delete Item 1 (with Id1=1) from Table A such that:</p>
  
  <pre><code>Table A:
    2,2,B
    3,3,C
  
  Table B:
    6,2,0,0,3
  </code></pre>
  
  <p>Can this be done automatically (eg. through triggers, etc.) in SQL or would I need to handle it 'manually' outside of the database?</p>