MySQL 触发设置工资 insert/update 限制
MySQL trigger to set salary insert/update limits
所以我有 table 名员工,我正在努力确保员工的收入不能超过他们的经理。
这是我尝试为此创建触发器的尝试
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%”这样的员工,因为我的非经理员工 ID 以 1 开头。
但没有任何效果,并且 MySQL 不断向我显示错误,其中出现“其中 staffID < 200000”。
帮助或建议哪些替代方案可能有效,我们将不胜感激!
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
所以我有 table 名员工,我正在努力确保员工的收入不能超过他们的经理。
这是我尝试为此创建触发器的尝试
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%”这样的员工,因为我的非经理员工 ID 以 1 开头。
但没有任何效果,并且 MySQL 不断向我显示错误,其中出现“其中 staffID < 200000”。
帮助或建议哪些替代方案可能有效,我们将不胜感激!
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