【发布时间】:2014-12-06 00:21:17
【问题描述】:
我已经为 MySQL 开发了几个小时的触发器,但我不知道出了什么问题。
这是我的表结构:
CREATE TABLE IF NOT EXISTS `RentalVideo` (
`OrderID` int(11) NOT NULL,
`VideoBarcode` int(11) NOT NULL,
`RentalReturned` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`OrderID`,`VideoBarcode`),
KEY `RentalVideo_VideoBarcode_FK` (`VideoBarcode`)
)
以下是一些示例数据:
INSERT INTO `RentalVideo` (`OrderID`, `VideoBarcode`, `RentalReturned`) VALUES
(1, 223823, 0),
(1, 447956, 0),
(3, 705481, 0),
(4, 988908, 0),
(5, 143375, 0);
这是不工作的触发器:
CREATE
TRIGGER `RENT_FIVE_VIDEOS_MAX` BEFORE INSERT
ON `bollywoo_video`.`RentalVideo`
FOR EACH ROW BEGIN
-- variable declarations
DECLARE vRentedVideos int;
DECLARE vCustomer int;
-- trigger code
SELECT RentalOrder.CustID
FROM RentalOrder
WHERE RentalOrder.OrderID = NEW.OrderID
INTO vCustomer;
SELECT COUNT(*)
FROM RentalOrder, RentalVideo
WHERE RentalOrder.CustID = vCustomer
AND RentalVideo.RentalReturned = 0
AND RentalOrder.OrderId = RentalVideo.VideoID
INTO vRentedVideos;
IF vRentedVideos >= 5 THEN
CALL RAISE_APPLICATION_ERROR(-2000, 'Cannot checkout more than 5 videos');
END IF;
END
最后但并非最不重要的是,这是我得到的错误:
Error
SQL query:
CREATE TRIGGER `RENT_FIVE_VIDEOS_MAX` BEFORE INSERT ON `bollywoo_video`.`RentalVideo`
FOR EACH
ROW BEGIN -- variable declarations
DECLARE vRentedVideos INT;
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 6
错误似乎发生在 DECLARE vRentedVideos int;
之前【问题讨论】:
-
为什么在
bollywoo_video.RentalVideoRentalVideo行中使用''? -
这只是我通过查看其他表格尝试过的。我只使用了
RentalVideo,但它仍然不起作用,同样的错误。
标签: mysql sql database triggers