Constraint on an aggregate? (need help writing trigger, i think..) - Mailing list pgsql-general

From Isak Hansen
Subject Constraint on an aggregate? (need help writing trigger, i think..)
Date
Msg-id 6b9e1eb20607260313h1d142d44od650ab3b6e23023@mail.gmail.com
Whole thread Raw
Responses Re: Constraint on an aggregate? (need help writing trigger,
List pgsql-general
I have the following two tables:

create table a (
  id serial primary key,
);
create table b (
  id serial primary key,
  a_id int4 references a (id),
  amount decimal(16, 2)
);

and would like a constraint to guarantee that "sum(b.amount) = 0 group
by b.a_id".


From my testing so far, and this thread:

<http://groups.google.com/group/comp.databases/browse_thread/thread/c2ce2d61eecf5c6c/1e0fa71282aea7d8#1e0fa71282aea7d8>,
i think a trigger is the way to go.

Problem is, i have no idea where to go from here. Getting the model
nearly to to 3NF and writing some simple queries is about the extent
of my db-related skillset..

Anyone feel the calling..? ;)


Also, how would this kind of check affect performance? Table 'b' is
our ledger table, busiest one in the app i assume, while 'a' groups
related transactions and holds common info. We inherited the term
voucher for 'a', anyone know if that is(n't) appropriate?

If someone are interested, the actual tables are here:
a: http://trac.lodo.no/wiki/vouchers
b: http://trac.lodo.no/wiki/voucher_lines


Any feedback appreciated,
Isak

pgsql-general by date:

Previous
From: "A. Kretschmer"
Date:
Subject: Re: question about default value and timestamp
Next
From: Joachim Wieland
Date:
Subject: Re: ECPG. Badly stuck