2016-05-31 2 views
0

트리거 코드에 문제가 있습니다.
트리거를 작성한 후 Insert를 작성하여 트리거를 테스트했습니다. 하지만 내 삽입물에 오류가 발생합니다.삽입 트리거가 내 트리거로 인해 실패했습니다

오류 코드 : 1109. 필드 목록에 알 수없는 테이블 employees.

트리거 앞에 삽입물을 넣으면 모든 것이 완벽하게 작동합니다. 그러나이 삽입물이 트리거를 테스트하기를 원합니다.

drop database if exists kontrolno; 
    create database kontrolno; 
    use kontrolno; 
    CREATE TABLE departments(
    id TINYINT UNSIGNED PRIMARY KEY, 
    name CHAR(12) NOT NULL, 
    min_salary SMALLINT UNSIGNED NOT NULL, 
    max_salary SMALLINT UNSIGNED NOT NULL 
    ) ENGINE=InnoDB; 
    CREATE TABLE employees(
    id SMALLINT UNSIGNED PRIMARY KEY, 
    name VARCHAR(255) NOT NULL, 
    salary SMALLINT UNSIGNED NOT NULL, 
    department_id TINYINT UNSIGNED, 
    constraint FOREIGN KEY (department_id) 
    REFERENCES departments(id) 
    ) ENGINE=InnoDB; 

    insert into departments(id,name,min_salary,max_salary) 
    values(1,"qa", 800,2000), 
    (2,"jd",1200,3500); 


    DROP TRIGGER if exists checkSalary; 
    delimiter | 
    create trigger checkSalary before Insert on employees 
    for each row 
    begin 

    if(employees.salary>max_salary OR employees.salary<min_salary) 
    then signal sqlstate '45000' set MESSAGE_TEXT="the salary is not valide"; 
    end if; 
    end; 
    | 
    delimiter ; 

    insert into employees(id,name,salary,department_id) 
    values(1,"ivan", 200,1); 
+0

당신이 경우 (employees.salary> MAX_SALARY 또는 employees.salary Sandeep

답변

0

트리거 코드에 실수가있었습니다.
트리거 코드는 다음과 같아야합니다

DROP TRIGGER if exists checkSalary; 
delimiter | 
create trigger checkSalary before Insert on employees 
for each row 
begin 

if(new.salary>(select max_salary from departments where id=new.department_id) 
    OR 
    new.salary<(select min_salary from departments where id=new.department_id)) 
then signal sqlstate '45000' set MESSAGE_TEXT="the salary is not valid"; 

end if; 
end; 
| 
delimiter ; 
+0

대단히 감사합니다! –

관련 문제