mysql检查日期约束(触发) [英] mysql check date constraint (trigger)
问题描述
很抱歉再次打扰,我是MySql Maria DB的全新用户(也许也可以在触发器上使用),并希望添加检查功能以确保在此表中未插入任何过去的日期.我相信,唯一的方法Mysql是创建触发器.我尝试创建一个,但仍然遇到问题.有人可以告诉我我在这里做错了吗(也许我需要一杯浓咖啡):
Sorry for disturbing again, I completely new on MySql Maria DB (maybe on trigger as well) and looking to add check function to ensure no past date is inserted in this table. I believe that the only way to do it Mysql is to create a trigger. I have tried to create one and I keep on having problems. Can someone tell me what I am doing wrong here (maybe I need a strong coffee):
CREATE TRIGGER check_date BEFORE INSERT on Event
FOR EACH ROW
BEGIN
if new.date <= now() then
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Event cannot start in the past event cannot start now. Choose an ulterior date'
end
end if;
请确保我在码头上.每次系统看到分号时,都会结束我的查询.因此,我不能有2个分号.
Please that i'm on a terminal. Every time the system sees a semicolon it ends my query. I cannot therefore have 2 semicolons.
感谢您的帮助.我希望我说得足够清楚...
thanks for your help . I hope im clear enough...
推荐答案
您需要在查询之前更改定界符:
You need to change delimiter before the query:
delimiter //
CREATE TRIGGER check_date BEFORE INSERT on Event
FOR EACH ROW
BEGIN
IF new.date <= now() THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Event cannot start in the past event cannot start now. Choose an ulterior date';
END IF;
END;//
delimiter ;
此外,在执行事件(触发)的整个代码块之前,请先结束 IF
语句
Also, end your IF
statement before you END
the entire block of code to execute for an event(trigger)
这篇关于mysql检查日期约束(触发)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!