Subject Re: [firebird-support] What is better? use integrity referencial or triggers?
Author Jerome Bouvattier
Hi,



> for the check of the primary key, and the foreign key?, what is
> better?, declare it trought constraints?, or do trought declare
> triggers that do the job?.

This is exactly what targets the thread "Isolation level in CHECKs and
triggers" unlike the subject is saying.
I suggest you read it along with the article "Declarative referential
integrity vs triggers" from Claudio Valderama.
http://www.cvalde.net/document/declaRefIntegVsTrig.htm

Basically, you can safely use RI triggers to check record existence in
so-called static tables in which you never delete records. Provided the
referenced table has few values, you might even gain in performance, because
you will avoid a low selectivity index in the referencing table.
In other cases, you probably want to use decl. RI, because triggers won't
give the same level of integrity. It is just a question of time or luck
before your integrity gets broken. As you will learn in the mentionned
thread, this is due to fact that decl. RI and triggers run in different
transaction contexts.


> or.. if i have a foreign key? constraint?, with a constraint i can't
> specify the cardinality of the relation.
>
> the only way that i can check that is programing with triggers.
>
> It is fine?, or exist some recomendation?

I'm in the same boat as you. I'm waiting for Ann's final word on this. So,
check the thread...

If you ever gonna use triggers, you probably want to avoid count(*) wich
will kill your perf. Use EXITS instead :


if (new.PAIS_ID is not null) then
begin
if (not exists (select PAIS_ID
from PAISES
where PAISES.PAIS_ID = new.PAIS_ID)) then
begin
exception sdp_icpe;
end
end


Hope it helps.

--
Jerome