触发器案例二:数据确认
涨工资不能越长越少
/*
涨后的薪水 , 涨前的薪水都是对同一条数据进行操作,需要使用到行级触发器
:old和:new 代表同一条记录
:old 表示操作改行之前,这一行的值
:new 表示操作改行之后,这一行的值
*/
create or repalce trigger checksalary
before update
on emp
for each row
begin
/*if 涨后的薪水 < 涨前的薪水 then */
if :new.sal< :old.sal then
raise_application_error(-20002,'涨后的薪水不能小于涨前的薪水. 涨后的薪水:'||:new.sal||' 涨前的薪水:'||:old.sal);
end if;
end;
:old 与 :new 的用法
数据的确认
create or replace trigger trg
before update
on table
for each row
begin
if :new.sal<:old.sal then
raise_application_error(-20002,'降薪?');
end if;
end;
/
触发器:数据确认案例
触发器场景:数据的确认
create or replace trigger checksalary
before update
on emp
for each row
begin
if :new.sal < :old.sal then
raise_application_error(-20002,'**不能少于**');
end if;
end;
/
raise_application_error抛出错误
:old,:new分表表示语句级触发器中操作前的行的值和操作后的行的值
用法[ :old.column ]
数据确认场景
触发器的应用场景2:数据的确认。
创建触发器的语法
CREATE [OR REPLACE] TRIGGER 触发器名
{BEFORE|ALTER}
{DELETE|INSERT|UPDATE[OF 列名]}
ON 表名
[FOR EACH ROW [WHEN(条件)]]
PLSQL 块
触发器之数据确认:
认识::new 和:old区别 与行级触发器
涨工资不能越涨越少
:old 表示操作该行之前这一行的值
:new 表示操作该行之后这一行的值
create or replace trigger check_salary
before update on emp for each row
begin
if :new.sal<:odl.sal then
raise_application_error(-20002,'涨后薪水不能少于涨前薪水。 涨后薪水为:'||:new.sal ||'涨前的薪水:'||:old.sal);
end if;
end;
/
行级触发器之前和之后的值。