[英]Mysql trigger constraint check
I am not an expert with databases and I need help while I am doing my database project. 我不是数据库专家,在执行数据库项目时需要帮助。 I am using phpmyadmin version 4.1.14 as my database server.
我正在使用phpmyadmin 4.1.14版本作为我的数据库服务器。 I am required to have a constraint which will make sure that a reader cannot borrow more than 10 books from the library.
我必须要有一个约束条件,以确保读者不能从图书馆借阅超过10本书。 I am trying to set up a trigger that will run before the BORROWED table is updated to make sure the reader did not borrowed more than 10 books.
我正在尝试设置一个触发器,该触发器将在更新BORROWED表之前运行,以确保读者借阅的书籍不超过10本。 If the reader has borrowed 10 books already I want to display a message.
如果读者已经借了10本书,我想显示一条消息。
I can't get this to work as I keep getting error. 由于不断出现错误,因此无法正常工作。 Here is my SQL for trigger
这是我的触发器SQL
BEGIN
DECLARE msg varchar(255);
SELECT Count(ReaderID) as Counted from BORROWED
WHERE RDateTime=null
IF Counted=10 THEN
SET msg='Reader cannot borrow more than 10 books';
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT=msg;
END IF;
END
This is the error message I get. 这是我收到的错误消息。 MySQL said: #1064 You have an error in your SQL syntax;
MySQL说:#1064您的SQL语法错误; check the manual that corresponds to you MySQL server version for the right syntax yo use near 'IF Counted=10 THEN SET msq='REader cannot borrow more than 10 books';
检查与您的MySQL服务器版本相对应的手册,以获取正确的语法,并在'IF Counted = 10 THEN SET msq ='REader不能借阅超过10本书'附近使用; SIGNAL S' at line 5
第5行的SIGNAL S'
Thank you for help ! 谢谢你的帮助 !
Try defining Counted
as a variable: 尝试将
Counted
定义为变量:
BEGIN
DECLARE msg varchar(255);
IF 10 <= (select count(*) from borrowed where rDateTime is NULL) THEN
SET msg = 'Reader cannot borrow more than 10 books';
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT=msg;
END IF;
END;
Note that I also changed the = NULL
to is NULL
, the correct way to do a NULL
comparison. 请注意,我还将
= NULL
更改is NULL
,这是进行NULL
比较的正确方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.