I have two tables in an SQL database with fields as follows:
Table A: Key | Id1 | Name
Table B: Key | Id2 | Idx1 | Idx2 | Id1
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).
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.
For example ...
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
I now delete Item 1 (with Id1=1) from Table A such that:
Table A:
2,2,B
3,3,C
Table B:
6,2,0,0,3
Can this be done automatically (eg. through triggers, etc.) in SQL or would I need to handle it 'manually' outside of the database?