Deletion with MYSQL reference integrity

Asked

Viewed 268 times

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

  • 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 .

  • 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.

  • 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 ?

  • 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.

  • more information will be deleted or only generate an update in the @Bruno tables

  • All records will be deleted. If necessary I can create a small example for you!

  • Please do this ? @

  • If possible use my data as the example.

  • recommend using PDO, mysq_query will be discontinued.

Show 5 more comments

1 answer

1


As indicated in the comments you do not need to do this management manually. If your goal is to delete the logs from all tables you can use the option ON DELETE CASCADE.

Here’s a short example using two of your tables.

create table tbl_CLIENTES (
    idCliente bigint(20) unsigned NOT NULL
  , loginCliente varchar(60) NOT NULL default ''
  , nome varchar(250) NOT NULL default ''
  , PRIMARY KEY  (idCliente)
  , KEY `user_login_key` (loginCliente)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ;

create table tbl_CLIENTES_AGENDA 
(
    id  int unsigned NOT NULL
  , idCliente   bigint(20) unsigned NOT NULL
  , email varchar(60) NOT NULL default ''
  , PRIMARY KEY  (id)
  , FOREIGN KEY (idCliente) REFERENCES tbl_CLIENTES(idCliente) 
    ON UPDATE CASCADE ON DELETE CASCADE
)Engine=INNODB;

With this setting, when a client is deleted, all tables that depend on the table tbl_CLIENTES (have a Foreign key that refers to a column in the table tbl_CLIENTES) will be automatically updated, that is, the records corresponding to that client will be deleted as well.

insert into tbl_CLIENTES values
(1, 'manuel00', 'Manuel Silva'),
(2, 'maria', 'Maria Silva');

insert into tbl_CLIENTES_AGENDA values
(1, 1, '[email protected]'),
(2, 2, '[email protected]');

The contents of the tables are now:

select * from tbl_clientes;

idCliente    | loginCliente  | nome
1            | manuel        | Manuel Silva
2            | maria         | Maria Silva 

select * from tbl_clientes_agenda;    

id     |  idCliente  | email
1      |  1          | [email protected]
2      |  2          | [email protected] 

Now we erase one of the customers

delete from tbl_CLIENTES where idCliente = 1

And after that the contents of table tbl_CLIENTES_AGENDA is as follows:

select * from tbl_clientes_agenda; 

id     |  idCliente  | email
2      |  2          | [email protected] 

Now, just apply this concept to all your tables that have Foreign Key.

  • is giving 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

  • You have a syntax error: Not IN ('64,65,66,67,68,70,71') but IN (64,65,66,67,68,70,71)

  • 1

    Yeah, yeah, yeah, buddy.

  • 1

    I’m glad I could help! :)

Browser other questions tagged

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