Home > other >  How do I automatically delete all associated components while deleting a product in a SQL relational
How do I automatically delete all associated components while deleting a product in a SQL relational

Time:11-09

Is this possible? If so, does it happen automatically or do I need to config the definition of the foreign key in the component table properly?

CodePudding user response:

The foreign key(s) would need to be defined as ON DELETE CASCADE in order for this to occur. Generally I'd recommend against such a setting because can you imagine (say) deleting a row from your GENDER table and suddenly discovering that half of the millon rows in your CUSTOMER table just vanished, and similarly half of the 100 million rows in your CUSTOMER_SALES tables also went.... That's a career limiting move.

If the foreign keys are not defined as ON DELETE CASCADE you could still mine the data dictionary to wor out the relationships in order to build a "delete child before parent" mechanism for those rare scenarios where you might need this

  •  Tags:  
  • sql
  • Related