Subject Re: Avoiding a delete
Author rodbracher
Hi

Thanks for the feedback - yes Martijn you are correct - it is exactly
the right way around. So below are the two working ways to solve the
same problem. But now I have a further question.
The trigger method seems better since the app gets feedback, the FK
method seems better since it is best to have referential integ. With
my FK - On delete no action - could my app pick this up ? then I
would have the best of both worlds. ( Short of putting both in )

FK method

alter table sale
add foreign key (dealer)
references dealer (dealer)
on delete no action

alter table salesrep
add foreign key (dealer)
references dealer (dealer)
on delete no action

----------------------------------------------------------------------
Trigger Method

create exception cannot_delete_dealer
'Cannot delete dealer. There is a salesrep and/or sale linked to this
dealer';

set term ^ ;
create trigger dealer_bd for dealer before delete as
begin
if ((exists(select * from sale where
sale.dealer = old.dealer)) or
(exists(select * from salesrep where
salesrep.dealer = old.dealer)))
then exception cannot_delete_dealer;
end^
set term ; ^

--- In ib-support@y..., "Martijn Tonies" <m.tonies@u...> wrote:
> Why is this the wrong way round?
>
> I believe you said you have a table Sales with a column Dealer
that's
> pointing to table Dealers and a Dealer-record cannot be deleted if
> there are any Sales records (that is, records pointing to records
in the
> Dealers table) - exactly the right way round.
>
> If this is not the case, show some table metadata.
>
> Martijn Tonies
> InterBase Workbench - the developer tool for InterBase and Firebird
> http://www.interbaseworkbench.com
>
> Upscene Productions
> http://www.upscene.com
>
> "This is an object-oriented system.
> If we change anything, the users object."