phpMyAdmin 触发 gui 检查年龄

phpMyAdmin trigger gui checking age

我想在插入记录到 'employees' table 之前检查年龄是否大于 18 岁。我正在使用 phpMyAdmin gui 触发工具。但是当我在“定义”部分键入此脚本时出现以下错误。

BEGIN
    IF (DATEDIFF(CURRENT_DATE(),NEW.birth_date) < 6570) THEN
        RAISEERROR('Age is less than 18 years!',16,1)
        ROLLBACK
    END IF
END

请帮我解决这个问题。

RAISEERRORROLLBACK 用于 TSQL(Microsoft SQL 服务器)语法。

在MySQL的情况下,我们使用SIGNAL .. SET MESSAGE_TEXT ..在Trigger内部抛出异常:

BEGIN
    IF (DATEDIFF(CURRENT_DATE(),NEW.birth_date) < 6570) THEN

      -- Throw Exception
      SIGNAL SQLSTATE '45000' 
        SET MESSAGE_TEXT = 'Age is less than 18 years!';

    END IF;  -- A semicolon (delimiter) is missing here
END

To signal a generic SQLSTATE value, use '45000', which means “unhandled user-defined exception.”

The error values that are accessible after SIGNAL executes are the SQLSTATE value raised by the SIGNAL statement and the MESSAGE_TEXT and MYSQL_ERRNO items.