Subject Is this necessary?
Author Edson T. Marques
I use the ER-Win (Interbase Server Style) to model and
build a simple database that have two tables:
Tab1 and Tab2 where Tab1 is parent and Tab2 is child.

Tab2 have a foreign key (CodeT1) that references Tab1.


The ER-Win generated the following code:

------------------------------------------------------
CREATE TABLE Tab1 (
CodeT1 CHAR(18) NOT NULL,
Attrib1 CHAR(18)
);

CREATE UNIQUE INDEX XPKTab1 ON Tab1
(
CodeT1
);

ALTER TABLE Tab1 ADD PRIMARY KEY (ChaveT1);


CREATE TABLE Tab2 (
CodeT2 CHAR(18) NOT NULL,
CodeT1 CHAR(18) NOT NULL
);

CREATE UNIQUE INDEX XPKTab2 ON Tab2
(
CodeT2
);

ALTER TABLE Tab2 ADD PRIMARY KEY (CodeT2);

ALTER TABLE Tab2 ADD FOREIGN KEY (CodeT1) REFERENCES Tab1;

CREATE EXCEPTION ERWIN_PARENT_INSERT_RESTRICT 'Cannot INSERT Parent table because Child table exists.';
CREATE EXCEPTION ERWIN_PARENT_UPDATE_RESTRICT 'Cannot UPDATE Parent table because Child table exists.';
CREATE EXCEPTION ERWIN_PARENT_DELETE_RESTRICT 'Cannot DELETE Parent table because Child table exists.';
CREATE EXCEPTION ERWIN_CHILD_INSERT_RESTRICT 'Cannot INSERT Child table because Parent table does not exist.';
CREATE EXCEPTION ERWIN_CHILD_UPDATE_RESTRICT 'Cannot UPDATE Child table because Parent table does not exist.';
CREATE EXCEPTION ERWIN_CHILD_DELETE_RESTRICT 'Cannot DELETE Child table because Parent table does not exist.';

CREATE TRIGGER tD_Tab1 FOR Tab1 AFTER DELETE AS
DECLARE VARIABLE numrows INTEGER;
BEGIN
select count(*) from Tab2
where Tab2.CodeT1 = OLD.CodeT1 into numrows;
IF (numrows > 0) THEN
BEGIN
EXCEPTION ERWIN_PARENT_DELETE_RESTRICT;
END
END !!

CREATE TRIGGER tU_Tab1 FOR Tab1 AFTER UPDATE AS
DECLARE VARIABLE numrows INTEGER;
BEGIN
IF (OLD.CodeT1 <> NEW.CodeT1) THEN
BEGIN
select count(*) from Tab2
where Tab2.CodeT1 = OLD.CodeT1 into numrows;
IF (numrows > 0) THEN
BEGIN
EXCEPTION ERWIN_PARENT_UPDATE_RESTRICT;
END
END
END !!

CREATE TRIGGER tI_Tab2 FOR Tab2 AFTER INSERT AS
DECLARE VARIABLE numrows INTEGER;
BEGIN
select count(*) from Tab1
where NEW.CodeT1 = Tab1.CodeT1 into numrows;
IF (numrows = 0) THEN
BEGIN
EXCEPTION ERWIN_CHILD_INSERT_RESTRICT;
END
END !!

CREATE TRIGGER tU_Tab2 FOR Tab2 AFTER UPDATE AS
DECLARE VARIABLE numrows INTEGER;
BEGIN
select count(*) from Tab1
where NEW.CodeT1 = Tab1.CodeT1 into numrows;
IF (numrows = 0) THEN
BEGIN
EXCEPTION ERWIN_CHILD_UPDATE_RESTRICT;
END
END !!
---------------------------------------------------------------


My question:

Is the triggers generated by ER-Win really necessary?

In the script exists a statement:
"ALTER TABLE Tab2 ADD FOREIGN KEY (CodeT1) REFERENCES Tab1;"
that set the field CodeT1 of Tab2 as a foreign key that
references Tab1. Is it grantting the consistency and integrity of
my data base or the triggers will be necessary?



[Non-text portions of this message have been removed]