DELIMITER //
CREATE TRIGGER borrow_insert_2
AFTER INSERT ON borrow
FOR EACH ROW
BEGIN
DECLARE num INT;
SET num=SELECT num FROM book WHERE bookId =NEW.bookId;
IF num=0 THEN
ROLLBACK ;
ELSE
UPDATE book SET num=num-1 WHERE bookId=NEW.bookId ;
END IF ;
END //
DELIMITER ;
为什么一直报错
1 queries executed, 0 success, 1 errors, 0 warnings
查询:set num=SELECT num FROM book WHERE bookId =NEW.bookId; IF num=0 THEN ROLLBACK ; ELSE UPDATE book SET num=num-1 WHERE bookId=NEW....
错误代码: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT num FROM book WHERE bookId =NEW.bookId;
IF num=0 THEN
ROLLBACK ;
EL' at line 1
执行耗时 : 0 sec
传送时间 : 0 sec
总耗时 : 0 sec
Mysql的触发器不支持rollback,如果你的borrow表有唯一性标识的话,可以把rollback改成
delete from borrow where id = new.id
DELIMITER //
CREATE TRIGGER borrow_insert_2
AFTER INSERT ON borrow
FOR EACH ROW
BEGIN
DECLARE @num INT ;
SET @num=(SELECT num FROM book WHERE bookId =NEW.bookId);
IF @num<=0 THEN
DELETE FROM borrow WHERE bookId=NEW.bookId ;
ELSE
UPDATE book SET num=num-1 WHERE bookId=NEW.bookId ;
END IF ;
END //
DELIMITER ;
还是报错了
1 queries executed, 0 success, 1 errors, 0 warnings
查询:SET @num=(SELECT num FROM book WHERE bookId =NEW.bookId); IF @num<=0 THEN delete from borrow where bookId=NEW.bookId ; ELSE UPDA...
错误代码: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF @num<=0 THEN
delete from borrow where bookId=NEW.bookId ;
ELSE
UPDATE ' at line 2
执行耗时 : 0 sec
传送时间 : 0 sec
总耗时 : 0 sec
这个是borrow表
CREATE TABLE borrow(
studentId CHAR(7) NOT NULL,
bookId INT PRIMARY KEY,
borrowDate DATE,
state VARCHAR(3)CHECK (state IN('已还','未还')),
FOREIGN KEY (studentId) REFERENCES students(studentId),
FOREIGN KEY (bookId)REFERENCES book(bookId)
)
@zqhwmy:
声明的时候,变量前面别加@
还有你这里用bookId定位删除不对吧,把之前的记录都删了
DELIMITER //
CREATE TRIGGER borrow_insert_2
AFTER INSERT ON borrow
FOR EACH ROW
BEGIN
DECLARE num INT ;
SET @num=(SELECT num FROM book WHERE bookId =NEW.bookId);
IF @num<=0 THEN
DELETE FROM borrow WHERE bookId=NEW.bookId ;
ELSE
UPDATE book SET num=num-1 WHERE bookId=NEW.bookId ;
END IF ;
END //
DELIMITER ;
@foreversag: 可以了,非常感谢
SET num= (SELECT num FROM book WHERE bookId =NEW.bookId);
DELIMITER //
CREATE TRIGGER borrow_insert_2
AFTER INSERT ON borrow
FOR EACH ROW
BEGIN
DECLARE @num INT;
SET @num=(SELECT num FROM book WHERE bookId =NEW.bookId);
IF @num=0 THEN
ROLLBACK ;
ELSE
UPDATE book SET num=num-1 WHERE bookId=NEW.bookId ;
END IF ;
END //
DELIMITER ;
改了()和@
但是还是报错
1 queries executed, 0 success, 1 errors, 0 warnings
查询:SET @num=(SELECT num FROM book WHERE bookId =NEW.bookId); IF @num=0 THEN ROLLBACK ; ELSE UPDATE book SET num=num-1 WHERE bookId=...
错误代码: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF @num=0 THEN
ROLLBACK ;
ELSE
UPDATE book SET num=num-1 WHERE bookId=NEW' at line 2
执行耗时 : 0 sec
传送时间 : 0 sec
总耗时 : 0 sec