0
The world of design is as follows:
I have a client, where I register for him, errands, news, upload photos, where there are schedules too. Only when I’m about to delete it, I need to delete everything from it, because the bank won’t allow it for referential integrity. So far so good, but in the news section, I have another table that links the images of the news to that news, and this link is made by a table where I have the news code and the image code, in fact it is a link many to many. So that is the problem, for me to delete the field of the table images referring to that news it is necessary that I delete before this relationship between the image and the news, but if I delete, I am not able to know what publication is that image.
How can I fix this ?
Follow what I’ve done so far.
mysql_query("DELETE FROM tbl_CLIENTES_AGENDA WHERE COD_IDENT_CLIEN ='" . $COD_IDENT_CLIEN . "'");
mysql_query("DELETE FROM tbl_IMAGENS
WHERE COD_SEQUN_IMAGM IN
(
SELECT COD_SEQUN_IMAGM FROM tbl_PUBLICACOESxIMAGENS
WHERE COD_IDENT_PUBLI IN
(
SELECT COD_IDENT_CLIEN FROM tbl_PUBLICACOES
WHERE COD_IDENT_CLIEN = " . $COD_IDENT_CLIEN . "
)
)");
mysql_query("DELETE FROM tbl_PUBLICACOES WHERE COD_IDENT_CLIEN = '" . $COD_IDENT_CLIEN . "'");
mysql_query("DELETE FROM tbl_RECADOS WHERE COD_IDENT_CLIEN = '" . $COD_IDENT_CLIEN . "");
mysql_query("DELETE FROM tbl_CLIENTES_PF WHERE COD_IDENT_CLIEN ='" . $COD_IDENT_CLIEN . "'");
mysql_query("DELETE FROM tbl_CLIENTES WHERE COD_IDENT_CLIEN ='" . $COD_IDENT_CLIEN . "'");
$query = mysql_query("SELECT * FROM tbl_IMAGENS I INNER JOIN tbl_CLIENTESxIMAGENS CI ON I.COD_SEQUN_IMAGM = CI.COD_SEQUN_IMAGM WHERE COD_IDENT_CLIEN ='" . $COD_IDENT_CLIEN . "' ");
while ($linha2 = mysql_fetch_array($query)) {
$arquivo = "../uploads/{$linha2['TXT_FILEN_IMAGN']}";
unlink($arquivo);
}
mysql_query("DELETE FROM tbl_CLIENTESxIMAGENS WHERE COD_IDENT_CLIEN ='" . $COD_IDENT_CLIEN . '"');
mysql_query("DELETE FROM tbl_IMAGENS WHERE COD_SEQUN_IMAGM IN
(
SELECT COD_SEQUN_IMAGM FROM tbl_CLIENTESxIMAGENS WHERE COD_IDENT_CLIEN = '" . $COD_IDENT_CLIEN . "'
)");
$sql = mysql_query("SELECT C.COD_IDENT_CLIEN, P.COD_IDENT_PUBLI, I.COD_SEQUN_IMAGM, I.TXT_FILEN_IMAGN
FROM
(
SELECT * FROM tbl_CLIENTES WHERE COD_IDENT_CLIEN = '" . $COD_IDENT_CLIEN . "'
) C
LEFT JOIN
(
SELECT * FROM tbl_PUBLICACOES
) P
ON P.COD_IDENT_CLIEN = C.COD_IDENT_CLIEN
LEFT JOIN
(
SELECT * FROM tbl_PUBLICACOESxIMAGENS
) PI
ON PI.COD_IDENT_PUBLI = P.COD_IDENT_PUBLI
LEFT JOIN
(
SELECT * FROM tbl_IMAGENS
) I
ON I.COD_SEQUN_IMAGM = PI.COD_SEQUN_IMAGM WHERE I.COD_SEQUN_IMAGM is not NULL;
");
while ($linha = mysql_fetch_array($sql)) {
$arquivo = "../uploads/{$linha['TXT_FILEN_IMAGN']}";
unlink($arquivo);
}
It is necessary to follow an order for deletion and it is: Delete Calendar, Delete Imagexpublication, Delete Image, Delete Publication, Delete Scrapbook, Delete cliente_Pf and Delete client last.
An error is occurring when I delete the Post table Ximages, it accuses the following error:
11:18:57 DELETE FROM tbl_PUBLICACOESxIMAGENS WHERE COD_SEQUN_IMAGM IN ('64,65,66,67,68,70,71') 1 Row(s) affected, 1 Warning(s): 1292 Truncated incorrect DOUBLE value: '64,65,66,67,68,70,71' 0.374 sec
The query I made is as follows::
DELETE FROM tbl_PUBLICACOESxIMAGENS WHERE COD_SEQUN_IMAGM IN ('64,65,66,67,68,70,71')
You don’t necessarily need to delete the client, you can create a field where you tell if it is active/inactive
– KaduAmaral
But I already have this field, but this example is in case the guy doesn’t want that client anymore, you have to give this choice .
– Renan Rodrigues
In fact it is unlikely that you should delete the client.
DELETE
is the most poorly used SQL command. But if you’re going to do the damage, then do it completely, destroy everything that has to do with it, starting with what’s on the end. Only you know all that needs to be done. But I stress that the removal of an item should not be possible this way. If you have elements that depend on it, you should not delete it. Simple like that. It should only be allowed when it really has no dependencies. Seems to be an invented requirement.– Maniero
Because the idea is this, if the guy creates the wrong client, or deeper if the guy goes a while without putting content for that client, and it’s simply necessary to erase all the content for that particular person, because there’s no link anymore, These are the options I need to give this chance. And yes I know what you have to do, but if I delete the items from the public table I lose which are the images linked to that publication, there is some way to save them and then make a query comparing ?
– Renan Rodrigues
Why not let DBMS manage this for you? Search by ON DELETE CASCADE? In all tables that have a Foreign key use the "ON DELETE CASCADE" and when a record pain removed from the table "father" the child tables will be automatically updated.
– bruno
more information will be deleted or only generate an update in the @Bruno tables
– Renan Rodrigues
All records will be deleted. If necessary I can create a small example for you!
– bruno
Please do this ? @
– Renan Rodrigues
If possible use my data as the example.
– Renan Rodrigues
recommend using PDO, mysq_query will be discontinued.
– Ivan Ferrer