2017-11-12 16 views
0

그래서 직원이 있으며 직원이 관리자보다 더 많은 수입을 올릴 수 없도록 노력하고 있습니다. 급여 삽입/갱신 제한을 설정하는 MySQL 트리거

여기에 나는 또한 관리자가 아닌 내 직원 ID가 1로 시작하기 때문에 '1 %'처럼 직원 곳 말할 트리거를 조정할 시도하는

create trigger staffsalary before update on employees for each row begin 
if ((new.salary < 50000) where staffid < 200000) 
then signal sqlstate '45000' set message_text = 'A promotion is required 
for staff to earn above 50k'; end if; end^^ 

에 대한 트리거를 작성하는 나의 시도이다.

하지만 아무 것도 작동하지 않으며, MySQL은 내가 어디에 staffID가 < (200000)인지 오류를 계속 표시합니다.

다른 대안이 도움이 될만한 제안이나 도움을 받으실 수 있습니다.

+0

,하지만 불행히도 모든 –

+0

에 내 질문에 대답하지 않습니다 ((new.salary <50000) staffid <200000 어디) 조건을 사용할 수없는 경우, 분명히 잘못에 문을 볼 경우 HTTPS의 경우 ://dev.mysql.com/doc/refman/5.7/en/if.html과로 변경하십시오. –

+0

<50000이 메시지 텍스트와 충돌하고 왜 NEW.staffid를 포기하여 staffid (트리거가 알지 못하는)에 대한 찬성을 나타 냈습니다. –

답변

0
MariaDB [sandbox]> delimiter $$ 
MariaDB [sandbox]> 
MariaDB [sandbox]> create trigger staffsalary before update on employees for each row begin 
    -> if ((new.salary > 50000) and new.emp_no < 5) then 
    -> signal sqlstate '45000' 
    -> set message_text = 'A promotion is required for staff to earn above 50k'; 
    -> end if; 
    -> end $$ 
Query OK, 0 rows affected (0.05 sec) 

MariaDB [sandbox]> 
MariaDB [sandbox]> delimiter ; 
MariaDB [sandbox]> 
MariaDB [sandbox]> select emp_no, salary from employees; 
+--------+--------+ 
| emp_no | salary | 
+--------+--------+ 
|  1 | 20000 | 
|  2 | 39500 | 
|  3 | 50000 | 
|  4 | 19500 | 
|  5 | 10000 | 
|  6 | 19500 | 
|  7 | 40000 | 
|  9 | NULL | 
+--------+--------+ 
8 rows in set (0.00 sec) 

MariaDB [sandbox]> 
MariaDB [sandbox]> update employees set salary = 66000 where emp_no = 1; 
ERROR 1644 (45000): A promotion is required for staff to earn above 50k 
감사 @Kevin