Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: SQL Rule



On Tue, Apr 25, 2006 at 02:27:23PM -0700, Bert wrote:
> I have a table construction like the one seen below, when i am updating
> or inserting i get a recurion, logical. But how to manage it that the
> rule is just doing it one time. Or is it possible to do the sum of a
> and b in an other way?
> ...
Bert,

(This is a resend to the list; I sent my reply privately by mistake).

Have you considered using a view to do the sums on the fly?  This
avoids all kinds of denormalization troubles (the sum can never be
incorrect):

wayne=# create table test (a int, b int);
CREATE TABLE
wayne=# create view test_sum as select *, a + b as c from test;
CREATE VIEW
wayne=# insert into test (a, b) values (1, 2);
INSERT 0 1
wayne=# insert into test (a, b) values (3, 4);
INSERT 0 1
wayne=# select * from test_sum;
 a | b | c
---+---+---
 1 | 2 | 3
 3 | 4 | 7
(2 rows)



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group