Skip to content
Advertisement

How to validate filleds in mysql for date today and onwards?

I am trying to use the following code

alter table Appointment ADD constraint chk_date check( AppointmentDate >= curdate());   

However it gives the following error saying that curdate function is disallowed in check. – Error

Advertisement

Answer

Only deterministic functions may be used in CHECK constraint expression. You cannot create such CHECK.

Use BEFORE INSERT trigger:

CREATE TRIGGER check_AppointmentDate 
BEFORE INSERT
ON Appointment 
FOR EACH ROW
BEGIN
IF NEW.AppointmentDate < CURRENT_DATE THEN
    SIGNAL SQLSTATE '45000' 
           SET MESSAGE_TEXT = 'AppointmentDate cannot be set in the past.';
END IF;
END

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=9bc964fbaab469ec841e62be99db4efd

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement