Subject Re: [ib-support] Master-Detail constraint checking
Author Helen Borrie
At 09:14 AM 28/04/2003 +0700, you wrote:
>Dear all,
>
>Let's assume a classic accounting database where you have a
>master-detail relationship between JOURNAL and JOURNALDETAIL. One of the
>rules says that for every JOURNAL, the sum of (JournalDetail.debit) must
>be equal to the sum of (JournalDetail.credit). But normally this rule is
>only satisfied by the end of the posting sequence. Here's a sample
>posting sequence (in pseudocode):
>
>...
> transaction begin;
>
> journal.insert; //constraint ok here, no details yet
> journaldetail.insert(debit = 100); //constraint fail here.
> journaldetail.insert(credit = 40); //constraint fail here.
> journaldetail.insert(credit = 60); //constraint ok here.
>
> transaction commit;
>...
>
>My question is: "is there any server-side constructs that we can use to
>enforce this 'rule'" in firebird?

Use triggers to enforce the rules.
The trick is to not to post *anything* until you have performed the
validations - by doing the whole operation in one stored procedure. This
way, everything is done on the server and the *rules* that control the
order in which the updates/inserts occur are server based and driven by the
context of the SP.

Make use of After Insert and After Update triggers to enforce your rules -
which is possible if things are done in the correct order.

Use the powerful custom exception-handling capabilities to deal with
exceptions which you want your procedure to "eat", rather than throwing
back to the client; or with validation errors that the SP detects in the
input, that you WANT to throw back at the client rather than proceed.

heLen