I have two simple tables in Oracle SQL Developer (Train and Driver) each with a common attribute (driverid). Essentially what i want to do is to not allow any updates on a particular driver tuple in the driver table if their driverid attribute exists in the train table. I have tried to add the following constraint however it throws back an error of 'subquery not allowed here.'
alter table driver add constraint drivcheck CHECK
(NOT EXISTS(select driverid from train))
I did a bit of digging around and the general feeling is that this condition should be checked with a trigger so I have tried to create a trigger to do the job but not having a lot of success. The below trigger is what i have come up with already.
create trigger drivcheck4
before update on driver
for each row
begin
declare
cursor dri is
select driverid from train where 'N' IN
(select availability
from driver
inner join train on driver.driverid=train.driverid
);
dri2 NUMBER;
begin
open dri;
loop
fetch dri into dri2;
exit when dri%NOTFOUND;
if check (exists (select * from dri2)) THEN
//Throw Error Section
else
//Allow update operation to take place
end if;
end loop;
close dri;
end;
I'm aware the trigger may be performing something different other than as described, but this is just the result of me experimenting. My initial description is what i am trying to achieve. If anyone has any thoughts, I would be very grateful!
Andrew,
Is there any error that you are facing? Something along the following lines should work. If you could post some sample data as well, it would be easy to test.
testing:
Updating to a driver id that does not exist in train table.
--updating to a driver id that exists in train table, raises an error.