简体   繁体   English

Oracle 触发更新和插入使用:新和:旧

[英]Oracle Trigger befor update and insert using :new and :old

I'm practicing what I learned in PL/SQL I have a table "Client" that contains:我正在练习我在 PL/SQL 中学到的知识,我有一个表“客户端”,其中包含:

Client ( id, name, lastName, email, city, phone, commission, salary)客户(id、姓名、姓氏、email、城市、电话、佣金、工资)

The commission should always be lower than salary佣金应该永远低于工资

I'm asked to create an oracle trigger before insert and update to make sure that commission < salary so what I did is the following我被要求在插入和更新之前创建一个 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

I'm not sure that this is correct, because if the user didn't update the salary and the commission or updated just one of these two columns then what's going to be the value of:new.salary and:new.commission?我不确定这是否正确,因为如果用户没有更新工资和佣金,或者只更新这两列之一,那么:new.salary 和:new.commission 的值是多少?

How should I proceed?我应该如何进行? thank you in advance先感谢您

Trigger code you posted is invalid.您发布的触发代码无效。 When fixed (and with applied NVL function), it looks like this:修复后(并应用了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.

Testing:测试:

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>

Looks OK to me.对我来说看起来不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM