首页 > 解决方案 > MySQL:触发器产生 Table not found 异常

问题描述

我有以下 2 个数据库表:DEPT(Dnumber、Dname、Fused、Mgr_ssn、Budget)EMPLOYEE(Ssn、Ename、Bdate、Dno、Salary)

我想创建一个触发器来确保没有一个部门拥有超过 8 名员工。这是我到目前为止写的

    DELIMITER //
CREATE TRIGGER ensure_dept_count_on_insert BEFORE insert on `EMPLOYEE`
    FOR EACH ROW
    BEGIN
        DECLARE maxCount integer;
        set maxCount = (select count(*) as c from Employee where Dno = new.Dno);
        if maxCount = 9
        then SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Department cannot have more than 8 employees';
        end if;
    END //
DELIMITER ;

问题是当我插入 EMPLOYEE 时,它给了我 Table NOT found Exception 并且当删除触发器时插入成功。

标签: mysqldatabasetriggers

解决方案


推荐阅读