如何在MySQL中执行异常处理(参照完整性)

时间:2013-08-27 09:16:28

标签: mysql sql exception-handling

MySQL version: 5.6.12

大家好,

我有一个MySQL数据库,下面有几个表和外键。由于修订目的,我需要通过在表的Deleted字段中填写删除的时间戳来对记录执行软删除(null表示不删除)。现在我想创建一个触发器,如果​​删除了父项,则取消链接已删除的项目。为了澄清我已经制作了一个小的演示脚本:

表arnold_test.Customer:

CREATE SCHEMA IF NOT EXISTS `arnold_test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `arnold_test` ;

-- -----------------------------------------------------
-- Table `arnold_test`.`Customer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `arnold_test`.`Customer` (
  `CustomerId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(45) NULL,
  `Email` VARCHAR(45) NULL,
  `Phone` VARCHAR(45) NULL,
  `Deleted` DATETIME NULL,
  PRIMARY KEY (`CustomerId`))
ENGINE = InnoDB;

表arnold_test.Address

-- -----------------------------------------------------
-- Table `arnold_test`.`Address`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `arnold_test`.`Address` (
  `AddressId` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `CustomerId` INT UNSIGNED NOT NULL,
  `Street` VARCHAR(45) NULL,
  `ZipCode` VARCHAR(45) NULL,
  `State` VARCHAR(45) NULL,
  `City` VARCHAR(45) NULL,
  `Country` VARCHAR(45) NULL,
  `Deleted` DATETIME NULL,
  PRIMARY KEY (`AddressId`),
  INDEX `fk_Address_Customer_idx` (`CustomerId` ASC),
  CONSTRAINT `fk_Address_Customer`
    FOREIGN KEY (`CustomerId`)
    REFERENCES `arnold_test`.`Customer` (`CustomerId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

删除触发器:

USE `arnold_test`;

DELIMITER $$
USE `arnold_test`$$
CREATE
DEFINER=`root`@`%`
TRIGGER `arnold_test`.`NinjaDeleteTrigger_Customer`
BEFORE UPDATE ON `arnold_test`.`Customer`
FOR EACH ROW
BEGIN

    IF OLD.Deleted IS NULL AND NEW.Deleted IS NOT NULL THEN


        BEGIN


            DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' BEGIN
                UPDATE `arnold_test`.`Address` R
                SET    R.Deleted = NEW.Deleted
                WHERE
                    OLD.CustomerId = R.CustomerId            
                    AND R.Deleted IS NULL;
            END;

            UPDATE `arnold_test`.`Address` R
            SET
                R.CustomerId = NULL        
            WHERE
                NEW.CustomerId = R.CustomerId        
                AND R.Deleted IS NULL;


        END;



    END IF;

END$$


DELIMITER ;

现在的问题是,当我尝试从Customer表中删除记录时(使用UPDATE .. SET Deleted = UTC_TIMESTAMP ..;),由于以下错误,查询失败:

ERROR 1452: 1452: Cannot add or update a child row: a foreign key constraint fails (`arnold_test`.`Address`, CONSTRAINT `fk_Address_Customer` FOREIGN KEY (`CustomerId`) REFERENCES `Customer` (`CustomerId`) ON DELETE NO ACTION ON UPDATE NO ACTION)

当我使用GET DIAGNOSTICS CONDITION 1 @p1 = RETURNED_SQLSTATE; SELECT @p1;检索SQLSTATE代码时,响应为23000(在触发器中引用我的continue处理程序中的sqlstate)。我无法弄清楚我做错了什么。

我在MySQL上搜索异常处理但没有找到关键答案。有没有人有想法?

感谢您的帮助! : - )

亲切的问候, 阿诺德·皮斯托瑞斯

2 个答案:

答案 0 :(得分:0)

您不能设置CustomerId = null,因为地址表中的customerid列是FOREIGN键和customer表,customerid列是主键,主键没有空值。

        UPDATE `Address` R
        SET
        R.CustomerId = NULL 

答案 1 :(得分:0)

我选择重写我的PHP脚本。现在它检查引用的字段是否为NULLABLE。如果是这样,它将引用设置为NULL,否则它将删除整个记录。