使用 :new 和 :old 更新和插入之前的 Oracle 触发器
Oracle Trigger befor update and insert using :new and :old
我正在练习我在 PL/SQL 中学到的东西
我有一个 table “客户端”,其中包含:
Client ( id, name, lastName, email, city, phone, commission , salary)
佣金应始终低于工资
我被要求在插入和更新之前创建一个 oracle 触发器以确保 佣金 < 工资
所以我所做的是以下
Create Trigger verifySalary
Before insert, update
ON Client
for each row
begin
if :new.salary < :new.comm then
raise_application_error(-20555, "commission should be lower than salary");
end if
end
我不确定这是否正确,因为如果用户没有更新薪水和佣金,或者只更新了这两列之一,那么 :new.salary 的值将是多少和 :new.commission ?
我应该如何进行?
提前谢谢你
您发布的触发代码无效。固定后(并应用 NVL
函数),它看起来像这样:
SQL> create table client (name varchar2(10), commision number, salary number);
Table created.
SQL> create or replace trigger verifysalary
2 before insert or update on client
3 for each row
4 begin
5 if nvl(:new.salary, 0) < nvl(:new.commision, 0) then
6 raise_application_error(-20555, 'commision should be lower than salary');
7 end if;
8 end;
9 /
Trigger created.
测试:
SQL> insert into client (name, commision, salary) values ('Little', 10, null);
insert into client (name, commision, salary) values ('Little', 10, null)
*
ERROR at line 1:
ORA-20555: commision should be lower than salary
ORA-06512: at "SCOTT.VERIFYSALARY", line 3
ORA-04088: error during execution of trigger 'SCOTT.VERIFYSALARY'
SQL> insert into client (name, commision, salary) values ('Little', 10, 100);
1 row created.
SQL> update client set commision = 50;
1 row updated.
SQL> update client set commision = 500;
update client set commision = 500
*
ERROR at line 1:
ORA-20555: commision should be lower than salary
ORA-06512: at "SCOTT.VERIFYSALARY", line 3
ORA-04088: error during execution of trigger 'SCOTT.VERIFYSALARY'
SQL> select * from client;
NAME COMMISION SALARY
---------- ---------- ----------
Little 50 100
SQL> update client set salary = null;
update client set salary = null
*
ERROR at line 1:
ORA-20555: commision should be lower than salary
ORA-06512: at "SCOTT.VERIFYSALARY", line 3
ORA-04088: error during execution of trigger 'SCOTT.VERIFYSALARY'
SQL> update client set salary = 10;
update client set salary = 10
*
ERROR at line 1:
ORA-20555: commision should be lower than salary
ORA-06512: at "SCOTT.VERIFYSALARY", line 3
ORA-04088: error during execution of trigger 'SCOTT.VERIFYSALARY'
SQL>
我觉得不错。
我正在练习我在 PL/SQL 中学到的东西 我有一个 table “客户端”,其中包含:
Client ( id, name, lastName, email, city, phone, commission , salary)
佣金应始终低于工资
我被要求在插入和更新之前创建一个 oracle 触发器以确保 佣金 < 工资 所以我所做的是以下
Create Trigger verifySalary
Before insert, update
ON Client
for each row
begin
if :new.salary < :new.comm then
raise_application_error(-20555, "commission should be lower than salary");
end if
end
我不确定这是否正确,因为如果用户没有更新薪水和佣金,或者只更新了这两列之一,那么 :new.salary 的值将是多少和 :new.commission ?
我应该如何进行? 提前谢谢你
您发布的触发代码无效。固定后(并应用 NVL
函数),它看起来像这样:
SQL> create table client (name varchar2(10), commision number, salary number);
Table created.
SQL> create or replace trigger verifysalary
2 before insert or update on client
3 for each row
4 begin
5 if nvl(:new.salary, 0) < nvl(:new.commision, 0) then
6 raise_application_error(-20555, 'commision should be lower than salary');
7 end if;
8 end;
9 /
Trigger created.
测试:
SQL> insert into client (name, commision, salary) values ('Little', 10, null);
insert into client (name, commision, salary) values ('Little', 10, null)
*
ERROR at line 1:
ORA-20555: commision should be lower than salary
ORA-06512: at "SCOTT.VERIFYSALARY", line 3
ORA-04088: error during execution of trigger 'SCOTT.VERIFYSALARY'
SQL> insert into client (name, commision, salary) values ('Little', 10, 100);
1 row created.
SQL> update client set commision = 50;
1 row updated.
SQL> update client set commision = 500;
update client set commision = 500
*
ERROR at line 1:
ORA-20555: commision should be lower than salary
ORA-06512: at "SCOTT.VERIFYSALARY", line 3
ORA-04088: error during execution of trigger 'SCOTT.VERIFYSALARY'
SQL> select * from client;
NAME COMMISION SALARY
---------- ---------- ----------
Little 50 100
SQL> update client set salary = null;
update client set salary = null
*
ERROR at line 1:
ORA-20555: commision should be lower than salary
ORA-06512: at "SCOTT.VERIFYSALARY", line 3
ORA-04088: error during execution of trigger 'SCOTT.VERIFYSALARY'
SQL> update client set salary = 10;
update client set salary = 10
*
ERROR at line 1:
ORA-20555: commision should be lower than salary
ORA-06512: at "SCOTT.VERIFYSALARY", line 3
ORA-04088: error during execution of trigger 'SCOTT.VERIFYSALARY'
SQL>
我觉得不错。