1

MySQL version: 5.6.12

大家好,

我有一个 MySQL 数据库,下面有几个表和外键。由于修订目的,我需要通过在表的已删除字段中填写删除时间戳来对记录进行软删除(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 中的异常处理,但找不到关键答案。有人有什么想法吗?

谢谢你的帮助!:-)

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

4

2 回答 2

0

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

        UPDATE `Address` R
        SET
        R.CustomerId = NULL 
于 2013-08-27T10:19:08.777 回答
0

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

于 2013-09-11T09:20:10.413 回答