Subject Re: [firebird-support] Custom RI was Re: Possible bug with CHECK constraint
Author Helen Borrie
At 09:20 PM 3/10/2004 -0300, you wrote:

>German Pablo Gentile wrote:
>
> >CREATE TRIGGER conductores_BDO FOR CONDUCTORES ACTIVE BEFORE DELETE
> >POSITION 0
> >AS
> >begin
> > /* Trigger text */
> > if (OLD_IDCONDUCTOR ANY
> > (SELECT IDCONDUCTOR FROM DESPACHADO WHERE IDCONDUCTOR =
> >OLD_IDCONDUCTOR)
> > ) then
> > begin
> > exception EX_DESPACHADO_VEHICULO;
> > suspend;
> > end;
> >end
> >
> >
> >
>My sintax was wrong. That work:
>
> if (EXISTS
> (SELECT IDBOMBA FROM DESPACHADO
> WHERE DESPACHADO.IDBOMBA = bombas.idbomba)
> )
> then
> begin
> exception ex_despachado_bomba;
> end
>
>Theres any more eficient way to do that? the query takes a seconds
>before result.

It is also wrong. Assuming this is a BeforeDelete trigger on BOMBAS, your
trigger will all deletions to any records from this table if *any* record
in despachado has a matching record in bombas..

What you *really* want to do is test whether there are any records in
despachado that are dependent on the CURRENT record:

create trigger bd_bombas for bombas
active before delete
as
begin
if (exists (select 1 from despachado
where idbomba = OLD.idbomba)) then
exception ex_despachado_bomba;
end

Hint: you will also need Before Insert and Before Update triggers on
DESPACHADO to restrict despachado.idbomba to values that exist in BOMBAS.

./heLen