我有桌子:
DEPARTMETS 带有 dep_id(number)、dep_name(varchar)、manager_id
(number) 字段。
EMPLOYEES with employee_id(number)、name(varchar)、salary(number)、manager_id
(number)
我想创建一个触发器,当数据插入或更新到表EMPLOYEES时,它负责检查表DEPARTMENTS中是否存在manager_id
>
触发器可能是这样的:
create or update trigger manager_exists
before insert or update on employees
for each row
begin
if exists **new id** into Departments then
INSERT DATA IN EMPLOYEES
else
"Error: MANAGER_ID doesnt exists in Departments"
end if;
end manager_exists;
但我不知道如何创建这个触发器。 注意:我需要它作为触发器。帮助!
答案 0 :(得分:1)
我是这样理解问题的:
create or replace trigger manager_exists
before insert or update on employees
for each row
declare
l_mgr number;
begin
select 1
into l_mgr
from dual
where exists (select null
from departments d
where d.manager_id = :new.manager_id
);
exception
when no_data_found then
raise_application_error(-20000, 'That manager does not exist in DEPARTMENTS');
end manager_exists;
换句话说:
MANAGER_ID
表中的 EMPLOYEES
是否存在于 DEPARTMENTS
表中
MANAGER_ID
你试图插入到 DEPARTMENTS
中是否存在 EMPLOYEES
表......但这不是你写的(或我误解了你的意思)insert
或 update
语句将完成这项工作