Możesz sprawdzić to ograniczenie w wyzwalaczu after insert lub update na tabeli curses.
CREATE or replace TRIGGER check_leader
AFTER INSERT OR UPDATE ON Course
FOR EACH ROW
declare
v_type varchar2(30);
BEGIN
select type into v_type from stuff where :NEW.leader_id = stuff.stuff_id;
if v_type != 'teacher' then
RAISE_APPLICATION_ERROR(-20000, 'course leader must be teacher');
end if;
end;
/
Ale potrzebujesz innego wyzwalacza w tabeli personelu. W przypadku zmiany typu rzeczy (z nauczyciela na sprzątacz ) należy sprawdzić wpisy w tabeli curses.
CREATE or replace TRIGGER check_courses
AFTER UPDATE ON STUFF
FOR EACH ROW
declare
v_num number;
BEGIN
if :OLD.type = 'teacher' and :NEW.type != 'teacher' then
select count(*) into v_num from curses where courses.leader_id = :NEW.stuff_id;
if v_num > 0 then
RAISE_APPLICATION_ERROR(-20000, 'there are courses assigned ');
end if;
end if;
end;
/