Re: SQL Rule

From: Alban Hertroys <alban(at)magproductions(dot)nl>
To: Bert <clemens(dot)bertschler(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL Rule
Date: 2006-04-26 09:48:14
Message-ID: 444F41DE.7080402@magproductions.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Bert wrote:
> Hi list
>
> 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?
>
> CREATE TABLE test
> (
> a int2,
> b int2,
> c int2,
> id int2 NOT NULL,
> CONSTRAINT id_test PRIMARY KEY (id)
> )
> WITHOUT OIDS;

You do know you can write this like this?:
CREATE TABLE test
(
a int2,
b int2,
c int2,
id int2 NOT NULL PRIMARY KEY
)
WITHOUT OIDS;

> CREATE OR REPLACE RULE sum_op AS
> ON INSERT TO test DO UPDATE test SET c = new.a + new.b
> WHERE test.id = new.id;

How do you expect to update a record that doesn't exist yet?

I suppose what you meant is something like this (didn't check the
syntax, but the INSTEAD part is important):

CREATE OR REPLACE RULE sum_op AS
ON INSERT TO TEST DO INSTEAD
INSERT (a, b, c, id) VALUES (new.a, new.b, new.a + new.b, new.id);

But as others suggested, a view is probably the better way to go.

Regards,
--
Alban Hertroys
alban(at)magproductions(dot)nl

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
7500 AK Enschede

// Integrate Your World //

In response to

  • SQL Rule at 2006-04-25 21:27:23 from Bert

Browse pgsql-general by date

  From Date Subject
Next Message Florian G. Pflug 2006-04-26 10:21:17 Re: Automatically assuming a specific role after connecting
Previous Message Richard Huxton 2006-04-26 08:58:14 Re: Cascade constraint renames?