0

我创建了一个存储过程,以便从不同的表中删除多行。基本上有一个主行,所有其他行通过外键连接。当我在有一个主行的情况下调用该程序时,一切正常,仅主行,其他行就消失了。但是当有不止一行时,存储过程不起作用。(没有外键交叉,这意味着在某些地方不能有两个具有相同 FK 的主行)。如果我删除所有回滚,我会得到“无法删除或更新父行:外键约束失败”,这很奇怪,因为当我直接以相同的顺序执行相同的删除语句(没有存储过程)时,即使使用多个主行。任何想法??这是我的存储过程

    -- --------------------------------------------------------------------------------
    -- Routine DDL
    -- Note: comments before and after the routine body will not be stored by the server
    -- --------------------------------------------------------------------------------
    DELIMITER $$

    CREATE DEFINER=`root`@`localhost` PROCEDURE `Tik_Del`(IN dell_id INT(11))
    BEGIN
    DECLARE hatan_id INT(11) DEFAULT -1;
    DECLARE cala_id INT(11) DEFAULT -1;
    DECLARE tashlum_id INT(11) DEFAULT -1;
    DECLARE teauda_id INT(11) DEFAULT -1;
    DECLARE haderacha_id INT(11) DEFAULT -1;
    DECLARE rav_mekadesh_id INT(11) DEFAULT -1;
    DECLARE pirtei_hatuna_id INT(11) DEFAULT -1;
    DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
    DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
    START TRANSACTION;

    SET SQL_SAFE_UPDATES=0;
    SELECT `tikim`.`hatan_id`,
        `tikim`.`cala_id`,
        `tikim`.`taslum_id`,
        `tikim`.`teauda_id`,
        `tikim`.`hadracha_id`,
        `tikim`.`rav_mekadesh_id`,
        `tikim`.`pirtei_hatuna_id`
    INTO hatan_id,cala_id,tashlum_id,teauda_id,haderacha_id,rav_mekadesh_id,pirtei_hatuna_id
    FROM `ad120`.`tikim` WHERE `tik_id`=dell_id;

    DELETE FROM `ad120`.`tikim` WHERE `tik_id`=dell_id;

    DELETE FROM `ad120`.`haderachot` WHERE `haderacha_id`=haderacha_id;

    DELETE FROM `ad120`.`tashlumim` WHERE `tashlum_id`=tashlum_id;

    DELETE FROM `ad120`.`rav_mekadesh` WHERE `rav_mekadesh_id`=rav_mekadesh_id;

    DELETE FROM `ad120`.`pirtei_hatuna` WHERE `pirtei_hatuna_id`=pirtei_hatuna_id;

    Call Aish_Del(hatan_id);
    Call Aish_Del(cala_id);

    Call Teauda_Del(teauda_id);


    COMMIT;
    END
4

2 回答 2

0

好的,我不知道为什么,但是当我将其更改为此时,它可以工作

    -- --------------------------------------------------------------------------------
    -- Routine DDL
    -- Note: comments before and after the routine body will not be stored by the server
    -- --------------------------------------------------------------------------------
    DELIMITER $$

    CREATE DEFINER=`root`@`localhost` PROCEDURE `Tik_Del`(IN dell_id INT(11))
    BEGIN
    /*DECLARE hatan_id INT(11) DEFAULT -1;
    DECLARE cala_id INT(11) DEFAULT -1;
    DECLARE tashlum_id INT(11) DEFAULT -1;
    DECLARE teauda_id INT(11) DEFAULT -1;
    DECLARE haderacha_id INT(11) DEFAULT -1;
    DECLARE rav_mekadesh_id INT(11) DEFAULT -1;
    DECLARE pirtei_hatuna_id INT(11) DEFAULT -1;*/
    DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
    DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
    set @hatan_id =-1;
    set @cala_id =-1;
    set @tashlum_id =-1;
    set @teauda_id =-1;
    set @haderacha_id=-1;
    set @rav_mekadesh_id =-1;
    set @pirtei_hatuna_id=-1;

    START TRANSACTION;

    SET SQL_SAFE_UPDATES=0;
    SELECT `tikim`.`hatan_id`,
        `tikim`.`cala_id`,
        `tikim`.`taslum_id`,
        `tikim`.`teauda_id`,
        `tikim`.`hadracha_id`,
        `tikim`.`rav_mekadesh_id`,
        `tikim`.`pirtei_hatuna_id`
    INTO         @hatan_id,@cala_id,@tashlum_id,@teauda_id,@haderacha_id,@rav_mekadesh_id,@pirtei_hatuna_id
    FROM `ad120`.`tikim` WHERE `tik_id`=dell_id;

    DELETE FROM `ad120`.`tikim` WHERE `tik_id`=dell_id;

    DELETE FROM `ad120`.`haderachot` WHERE `haderacha_id`=@haderacha_id;

    DELETE FROM `ad120`.`tashlumim` WHERE `tashlum_id`=@tashlum_id;

    DELETE FROM `ad120`.`rav_mekadesh` WHERE `rav_mekadesh_id`=@rav_mekadesh_id;

    DELETE FROM `ad120`.`pirtei_hatuna` WHERE `pirtei_hatuna_id`=@pirtei_hatuna_id;

    Call Aish_Del(@hatan_id);
    Call Aish_Del(@cala_id);

    Call Teauda_Del(@teauda_id);


    COMMIT;
    END        
于 2013-09-29T08:23:21.963 回答
0

你有没有考虑过做一个多表DELETE?

DELETE k, h, t, r, p 
FROM `ad120`.`tikim` AS k
LEFT OUTER JOIN `ad120`.`haderachot` AS h USING (`hadracha_id`)
LEFT OUTER JOIN `ad120`.`tashlumim` AS t USING (`tashlum_id`)
LEFT OUTER JOIN `ad120`.`rav_mekadesh` AS r USING (`rav_mekadesh_id`)
LEFT OUTER JOIN `ad120`.`pirtei_hatuna` AS p USING (`pirtei_hatuna_id`)
WHERE k.`tik_id` = dell_id;

另一种选择是使用声明所有外键ON DELETE CASCADE。然后你可以删除tikim并让其余的级联(我假设这tikim是所有其他表引用的父级)。

于 2013-09-29T00:28:53.453 回答