Subject | What is better? use integrity referencial or triggers? |
---|---|
Author | newtonlistas |
Post date | 2003-11-26T20:32:41Z |
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?.
I mean, here exist a constraint primary key, but what if i define a
index unique?, what is better?
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?
/* ============================================================ */
create table ESTADO
(
ESTADO_ID INTEGER not null,
PAIS_ID INTEGER ,
ESTADO_CODIGO VARCHAR(5) ,
ESTADO_NOMBRE VARCHAR(32) ,
constraint pk_estado primary key (ESTADO_ID)
);
create exception sdp_icpe "Parent does not exist. Cannot create child.";
/* Insert trigger "ti_estado" for table "ESTADO" */
set term /;
create trigger ti_estado for ESTADO
before insert as
declare variable numrows integer;
begin
/* Parent "PAISES" must exist when inserting a child in "ESTADO" */
if (new.PAIS_ID is not null) then
begin
select count(*)
from PAISES
where PAISES.PAIS_ID = new.PAIS_ID
into :numrows;
if (numrows = 0) then
begin
exception sdp_icpe;
end
end
end;/
set term ;/
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?.
I mean, here exist a constraint primary key, but what if i define a
index unique?, what is better?
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?
/* ============================================================ */
create table ESTADO
(
ESTADO_ID INTEGER not null,
PAIS_ID INTEGER ,
ESTADO_CODIGO VARCHAR(5) ,
ESTADO_NOMBRE VARCHAR(32) ,
constraint pk_estado primary key (ESTADO_ID)
);
create exception sdp_icpe "Parent does not exist. Cannot create child.";
/* Insert trigger "ti_estado" for table "ESTADO" */
set term /;
create trigger ti_estado for ESTADO
before insert as
declare variable numrows integer;
begin
/* Parent "PAISES" must exist when inserting a child in "ESTADO" */
if (new.PAIS_ID is not null) then
begin
select count(*)
from PAISES
where PAISES.PAIS_ID = new.PAIS_ID
into :numrows;
if (numrows = 0) then
begin
exception sdp_icpe;
end
end
end;/
set term ;/