I am trying to write a SQL trigger for any insert on the relation Section
and it should ensure that the time slot id value that is being inserted is valid.
Thanks in advance!
I am trying to write a SQL trigger for any insert on the relation Section
and it should ensure that the time slot id value that is being inserted is valid.
Thanks in advance!
Let's start by stating that using triggers to enforce relational integrity instead of foreign key constraints is worst practice. RI triggers are slow, they don't scale well, they don't work in multi-user environments, they provoke needless head-scratching amongst the poor blighters who have to maintain the code.
So, this is what a functioning worst practice trigger would look like:
create or replace trigger section_timeslot_trg
before insert or update on section
for each row
declare
l_id timeslot.timeslot_id%type;
begin
select ts.timeslot_id
into l_id
from timeslot ts
where ts.timeslot_id = :new.timeslot_id;
exception
when no_data_found then
raise_application_error(-20999, 'Not a valid TIMESLOT_ID: '||:new.timeslot_id);
end;
Remember that in the absence of a foreign key constraint there would need to be a reciprocal trigger on TIMESLOT to prevent updates and deletes of TIMESLOT_IDs which are used in SECTION.