MySQL触发器设置工资插入/更新限制

时间:2017-11-12 12:26:22

标签: mysql triggers mysql-error-1064

所以我有一个餐桌员工,我正在努力确保员工的收入不能超过他们的经理。

这是我尝试为该

创建触发器
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^^

我还试图定制触发器,说出人员喜欢的地方,例如1%&#39;因为我的员工ID不是经理从1开始。

但是没有任何效果,并且MySQL一直向我显示错误,其中我有&#39;其中staffID&lt; 200000)。

可以提供帮助或建议可能有用的替代方案!

1 个答案:

答案 0 :(得分: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