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
関連する問題