How to use Cascade Delete between two tables?

2

I have 2 tables:

products

id

name

...

photo_products

id

product_id

photo

In PHP I will make a DELETE in the Product, and consequently I want to automatically delete ALL the photos of the deleted product, of course delete also the record in the table.

In the case, I must first fiddle with Cascade (SQL), then in PHP to delete the files, right?

    
asked by anonymous 26.01.2017 / 02:07

1 answer

1

When you put in your FK on DELETE CASCADE when you delete the table the effect will be cascade, ie if you delete the Product your photos will be deleted automatically and you do not need to delete in PHP because this is automatic in the bank. However cascade should be used with caution, if you still need some information from the table use SET NULL and when MAPPING FK leave with NULL.

    
26.01.2017 / 02:22