我正在建立一个数据库并尝试实施触发器。所有表和触发器都在没有警告的情况下上传到数据库中,但是当我在表操作符更新时激活触发器时,我收到一条错误消息
UPDATE operator SET `NAME_FIRST`='JOHN' WHERE `OPERATOR_ID`=0;
SQL Error (1109): Unknown table 'NEW' in field list
我怀疑它在审核员触发器中。
使用 HEIDISQL 导出数据库
-- --------------------------------------------------------
-- Host: 127.0.0.1
-- Server version: 8.0.32 - MySQL Community Server - GPL
-- Server OS: Win64
-- HeidiSQL Version: 12.8.0.6908
-- --------------------------------------------------------
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-- Dumping structure for table test.auditor
CREATE TABLE IF NOT EXISTS `auditor` (
`OPERATOR_ID` int NOT NULL,
`IS_ACTIVE` enum('TRUE','FALSE') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'TRUE',
PRIMARY KEY (`OPERATOR_ID`),
CONSTRAINT `auditor_FK_OPERATOR_ID` FOREIGN KEY (`OPERATOR_ID`) REFERENCES `operator` (`OPERATOR_ID`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Dumping data for table test.auditor: ~0 rows (approximately)
-- Dumping structure for procedure test.auditor_validation
DELIMITER //
CREATE PROCEDURE `auditor_validation`(
IN `OPERATOR_ID` INT
)
BEGIN
DECLARE myVar VARCHAR(5);
SELECT
`IS_ACTIVE` INTO myVar
FROM
operator
WHERE
operator.OPERATOR_ID = OPERATOR_ID;
IF NEW.ACTIVE = 'TRUE' AND myVar = 'FALSE' THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Operator is marked as inactive';
END IF;
END//
DELIMITER ;
-- Dumping structure for table test.operator
CREATE TABLE IF NOT EXISTS `operator` (
`OPERATOR_ID` int NOT NULL AUTO_INCREMENT,
`NAME_FIRST` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
`NAME_LAST` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
`BUSINESS_ID` char(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
`IS_ACTIVE` enum('TRUE','FALSE') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'TRUE',
PRIMARY KEY (`OPERATOR_ID`),
KEY `BUSINESS_ID` (`BUSINESS_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Dumping data for table test.operator: ~2 rows (approximately)
INSERT INTO `operator` (`OPERATOR_ID`, `NAME_FIRST`, `NAME_LAST`, `BUSINESS_ID`, `IS_ACTIVE`) VALUES
(-1, 'JANE', 'DOE', '456XYZ', 'TRUE'),
(2, 'JOE', 'DOE', 'ABC123', 'FALSE');
-- Dumping structure for trigger test.auditor_before_insert
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
DELIMITER //
CREATE TRIGGER `auditor_before_insert` BEFORE INSERT ON `auditor` FOR EACH ROW BEGIN
CALL auditor_validation(NEW.OPERATOR_ID);
END//
DELIMITER ;
SET SQL_MODE=@OLDTMP_SQL_MODE;
-- Dumping structure for trigger test.auditor_before_update
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
DELIMITER //
CREATE TRIGGER `auditor_before_update` BEFORE UPDATE ON `auditor` FOR EACH ROW BEGIN
CALL auditor_validation(NEW.OPERATOR_ID);
END//
DELIMITER ;
SET SQL_MODE=@OLDTMP_SQL_MODE;
-- Dumping structure for trigger test.operator_after_update
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
DELIMITER //
CREATE TRIGGER `operator_after_update` AFTER UPDATE ON `operator` FOR EACH ROW BEGIN
DECLARE OP_ID INT;
SET OP_ID = NEW.OPERATOR_ID;
IF NEW.IS_ACTIVE = 'FALSE' THEN
UPDATE auditor
SET auditor.IS_ACTIVE = 'FALSE'
WHERE auditor.OPERATOR_ID = OP_ID;
END IF;
END//
DELIMITER ;
SET SQL_MODE=@OLDTMP_SQL_MODE;
/*!40103 SET TIME_ZONE=IFNULL(@OLD_TIME_ZONE, 'system') */;
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */;
我尝试替换触发器中的
NOW.%Value%
,但这有时会阻止错误出现,但并非总是如此
编辑:错误也显示为
insert into auditor (`Operator_ID`,`IS_ACTIVE`) VALUES (2, 'FALSE');
。
显然,错误仅在审核员表发生更改或审核员记录的父级发生更改时显示,明确指向该过程
我发现了导致问题的
NEW
引用,作为auditor_validation过程的一部分,将其更改为参数修复了问题并允许插入审计表并更新操作员表