2
On my system, when deleting a post, all comments that have the id_publicacao
same as the publication are deleted from a table called comentarios_publicacao
. And I do it simply by doing a check like:
if(deletar a publicacao) {
então eu apago todos os comentários com a id_publicacao igual ao da publicacao que foi apagada
}
So I was doing some Google searches when I came across the famous Foreign Key
then I researched about. But I didn’t understand very well... I also saw something related to Cascade
that if I erased something in the "father table", everything related to it in "daughter tables" would also be erased. Could I replace the method I use (as well as the "example" I gave above) with that of the daughter tables etc? And how would I do that? As this subject is new to me I’m still a little lost, I saw several explanations on blogs and sites about Foreign Key
but I couldn’t really understand when to use it. Could someone give me a clear and direct explanation about the concept of Chave Estrangeira
and derivatives thereof (type, what are constraints, Scade, etc...)?
Thanks for the reply, I finally understood something hehe. Just a doubt: In the example you gave using the
CASCADE
it will only delete whatever has thecomentarios_publicacao(id_publicacao)
equal to that of thepublicacao(id)
correct?– Igor
Exactly. Eliminate the "children" whose foreign key is equal to the primary key of the deleted "father".
– ramaral