На основе образца схемы Скотта я создаю таблицы EMP_T и DEPT_T, которые содержат только данные;без ограничений.
SQL> create table emp_t as select * From emp;
Table created.
SQL> create table dept_t as select * From dept;
Table created.
Триггер:
SQL> create or replace trigger trg_biu_emp_d
2 before insert or update on emp_t
3 for each row
4 declare
5 l_deptno dept_t.deptno%type;
6 begin
7 select d.deptno
8 into l_deptno
9 from dept_t d
10 where d.deptno = :new.deptno;
11 exception
12 when no_data_found then
13 raise_application_error(-20000, 'That department does not exist');
14 end;
15 /
Trigger created.
SQL>
Тестирование: сначала ошибки:
SQL> update emp_t set deptno = 50;
update emp_t set deptno = 50
*
ERROR at line 1:
ORA-20000: That department does not exist
ORA-06512: at "SCOTT.TRG_BIU_EMP_D", line 10
ORA-04088: error during execution of trigger 'SCOTT.TRG_BIU_EMP_D'
SQL> insert into emp_t (empno, ename, deptno) values (999, 'Littlefoot', 50);
insert into emp_t (empno, ename, deptno) values (999, 'Littlefoot', 50)
*
ERROR at line 1:
ORA-20000: That department does not exist
ORA-06512: at "SCOTT.TRG_BIU_EMP_D", line 10
ORA-04088: error during execution of trigger 'SCOTT.TRG_BIU_EMP_D'
SQL>
Тестирование №2: успех:
SQL> update emp_t set deptno = 10;
14 rows updated.
SQL> insert into emp_t (empno, ename, deptno) values (999, 'Littlefoot', 20);
1 row created.
SQL>