How to use Cascade Delete between two tables?

Asked

Viewed 468 times

2

I have 2 tables:

products

id

name

...

products_photos

id

id_product

photo

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

inserir a descrição da imagem aqui

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

1 answer

1


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.