Subject | Keeping count of child records in master record |
---|---|
Author | ainpoissee |
Post date | 2008-10-29T15:50:35Z |
Hi,
I'd like to have number of detail records in master record, so that
for each master record I know would I need to query for detail
records... seems easy:
CREATE TABLE TAB_Master (
UID INTEGER PRIMARY KEY,
...
DetailCnt INTEGER NOT NULL CHECK(DetailCnt >= 0)
);
CREATE TRIGGER SyncCnt_I FOR TAB_Detail
ACTIVE AFTER INSERT POSITION 1000
AS
BEGIN
UPDATE TAB_Master SET DetailCnt = DetailCnt + 1 WHERE(UID =
NEW.MasterID);
END^
CREATE TRIGGER SyncCnt_D FOR TAB_Detail
ACTIVE AFTER DELETE POSITION 1000
AS
BEGIN
UPDATE TAB_Master SET DetailCnt = DetailCnt - 1 WHERE(UID =
OLD.MasterID);
END^
but is it safe?
I vaguely recall that there were some problems with FB implementation
of UPDATE statement like these... or is there perhaps some scenario
which might cause the DetailCnt field to be out of sync with real
detail count? Any better way to implement this?
TIA
ain
I'd like to have number of detail records in master record, so that
for each master record I know would I need to query for detail
records... seems easy:
CREATE TABLE TAB_Master (
UID INTEGER PRIMARY KEY,
...
DetailCnt INTEGER NOT NULL CHECK(DetailCnt >= 0)
);
CREATE TRIGGER SyncCnt_I FOR TAB_Detail
ACTIVE AFTER INSERT POSITION 1000
AS
BEGIN
UPDATE TAB_Master SET DetailCnt = DetailCnt + 1 WHERE(UID =
NEW.MasterID);
END^
CREATE TRIGGER SyncCnt_D FOR TAB_Detail
ACTIVE AFTER DELETE POSITION 1000
AS
BEGIN
UPDATE TAB_Master SET DetailCnt = DetailCnt - 1 WHERE(UID =
OLD.MasterID);
END^
but is it safe?
I vaguely recall that there were some problems with FB implementation
of UPDATE statement like these... or is there perhaps some scenario
which might cause the DetailCnt field to be out of sync with real
detail count? Any better way to implement this?
TIA
ain