Constraint on an aggregate? (need help writing trigger, i think..)

From: "Isak Hansen" <isak(dot)hansen(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Constraint on an aggregate? (need help writing trigger, i think..)
Date: 2006-07-26 10:13:36
Message-ID: 6b9e1eb20607260313h1d142d44od650ab3b6e23023@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joachim Wieland 2006-07-26 10:29:09 Re: ECPG. Badly stuck
Previous Message alan 2006-07-26 06:44:35 Re: Invalid column display size. Cannot be less than zero