-- Create a trigger that will prevent an update to a student table if EnrolledDate is in the future
USE College ;
DROP TRIGGER IF EXISTS Student_Before_Update;
DELIMITER $$
CREATE TRIGGER Student_Before_Update
BEFORE UPDATE ON Student
FOR EACH ROW
BEGIN
\t IF NEW.EnrolledDate > '2016-10-18' THEN
\t \t \t \t SIGNAL SQLSTATE VALUE '45000'
\t \t \t \t SET MESSAGE_TEXT= 'Enrolled date may not be in the future';
\t \t END IF;
END $$
DELIMITER ;
UPDATE Student
SET EnrolledDate= DATE(now())
WHERE ID= 1;
UPDATE Student
SET EnrolledDate= DATE_ADD(now(),INTERVAL 1 DAY)
WHERE ID=1;
大家好,
我的工作創造一個BEFORE UPDATE觸發器如果我更新學生的EnrolledDate與將來的日期,應顯示錯誤 消息。例如,Student表中最後插入的EnrolledDate爲'2016-04-17',因此該日期之後的任何其他記錄都應標記錯誤消息,指出「註冊日期可能不在將來」。我幾個小時以來一直在努力研究這個問題,但我仍然無法得到。有人可以幫幫我嗎?這是我的代碼到目前爲止。
如何?您正在比較過去(2016-10-18)的硬編碼日期方式,而不是現在。 –
@Sloan Thrasher我應該使用GetDate()嗎?我嘗試過,但它不起作用。有什麼建議麼? – DSeals
'''DATE(NOW())'''應該可以工作。 –