I want to delete records of child table and its parent table however there is a catch in this, I have to delete the indirect child of that parent table too.
Let’s take an example to understand more on the problem:
My Database is very structural and I have specific names associated with some prefix like for Employee data all tables will start with
Emplyee*
tables.Each module is having same prefix set of tables. I have almost 20 sets of table for which I have to delete data based on type provided means if Employer is provided, I need to delete data of employer and its employees and its products selected by employees.
Employer_Details
has anID
(PK) which is reference in allEmployer_*
tables and also inEmployee_MainTable
.Employee_MainTable
has anemployee_ID
(PK) which is referenced in allEmployee_*
table same with product.
So when I pass Employer as table name and column name as ID to proc, it should delete all data from tables (Employer, Employee and Product) based above condition. If I pass Employee, then it should delete data from Employee_*
and Product_*
tables.
I have started a bit but want some expert comment to make it easy. I do not want to mention tables name instead of that I just need some dynamic query which will get all tables based on reference with one more column order number so that I will delete according to order number.
We don't have cascade delete as these tables are existing and we can't change them. I want to get this by using Information_Schema.Tables means dynamic.
Hope it’s not too complicated!! Thanks!!