MySQL8.0创建函数检测日期是否为闰年
下载MySQL Workbench
SET GLOBAL log_bin_trust_function_creators = 1;
use testdb;
DROP FUNCTION IF EXISTS `is_leap_year`;
DELIMITER $$
CREATE FUNCTION `is_leap_year` (birth_date date)
RETURNS BOOLEAN
READS SQL DATA #表示子程序包含读数据的语句 但不包含写数据的语句
DETERMINISTIC #指明函数或者存储过程的执行结果是否是确定的
BEGIN
DECLARE leap_year BOOLEAN;
SET leap_year = FALSE;
IF ((select MOD(YEAR(birth_date),4) = 0) AND (select MOD(YEAR(birth_date),100) !=0)) THEN
SET leap_year = TRUE;
ELSEIF (select MOD(YEAR(birth_date),400) = 0) THEN
SET leap_year = TRUE;
#ELSE
#SET leap_year = FALSE;
END IF;
RETURN leap_year;
END$$
DELIMITER ;
select is_leap_year(DATE('1996-12-29'));
select is_leap_year(DATE('2001-12-29'));
select is_leap_year(DATE('2000-12-29'));
select year(date('2008-12-12'));
常见错误及解决方案:
MySQL创建触发器的时候报1419错误( 1419 - You do not have the SUPER privilege and binary logging is enabled )
解决方法1:
第一步,用root用户登录:mysql -u root -p
第二步,设置参数log_bin_trust_function_creators为1:set global log_bin_trust_function_creators = 1;
再次尝试创建触发器,成功!(同样,在删除触发器报该错误的时候也是如此处理)
解决方法2:
导入数据的用户不仅需要CREATE ROUTINE, ALTER ROUTINE, CREATE TRIGGER, ALTER TRIGGER, CREATE FUNCTION 和 ALTER FUNCTION 权限,还需要SUPER privileges 权限,使用超级用户导入数据。