-1

I have a task set which requires the rooting and removal of demo accounts in a database. I have created a query where all the demo accounts are returned but there are fields within this query that have various children/keys. I now want to create a query where I collect all the demo accounts as well as return all entries where the dependencies are returned so a delete statement can be created and run to remove any entries that relate to these accounts.

What is the best method of retrieving all this data in one place so a delete statement can run off minimal code?

Here is my statement for finding the Demo accounts;

Select ct.id as 'cutomer id', c.id as 'client id', ct.name, da.demo_account_status, da.demo_expiry_date, c.enabled from client c
join customer ct on ct.client_id = c.id
join demo_account da on da.client_id = c.id
join demo_account_extend_survey das on das.demo_account_id = da.id
where da.demo_account_status = 'DEMO_ACCOUNT_EXPIRED'
and c.enabled = 0 
group by ct.id;

Example data of the below looks like this;

233477 26412 customer name DEMO_ACCOUNT_EXPIRED 2016-11-10 0
233478 26412 customer name DEMO_ACCOUNT_EXPIRED 2016-11-10 0
233479 26412 customer name DEMO_ACCOUNT_EXPIRED 2016-11-10 0
233480 26412 customer name DEMO_ACCOUNT_EXPIRED 2016-11-10 0

And the this attempted statement is for gathering all foreign key/children ID's in one place so that a delete can be done at a later stage.

select ct.id, p.id, c.id, cc.id, ca.id, si.id, isi.id, ct.billing_address_id from customer ct
join client c on c.id=ct.client_id
join person p on p.id = c.id
join customer_contact cc on cc.customer_id = ct.id
join customer_account ca on ca.customer_id = ct.id
join sales_invoice si on si.customer_id = ct.id
join issued_customer_credit_note_tx isi on isi.customer_id = ct.id
join address ad on ad.id = ct.billing_address_id
where c.id = 
  • Show us some sample table data and the expected result - all as well formatted text. Also show us your current query attempt(s). – jarlh Mar 30 '17 at 09:36
  • you show never delete the data unless and until you relay don't need. You should update the column with flag as false – Nishant Nair Mar 30 '17 at 09:40
  • @NishantNair Handling historical data is more complicated than just adding a status flag to a table. – reaanb Mar 30 '17 at 10:06

1 Answers1

0

Add a CASCADE DELETE constraint.

Read more on Cascade Delete

Give us some sample data and table so we can help you with your delete script

Community
  • 1
  • 1
beejm
  • 2,381
  • 1
  • 10
  • 19