Re: views and rules

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michiel Lange <michiel(at)minas(dot)demon(dot)nl>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: views and rules
Date: 2002-11-30 16:34:26
Message-ID: 418.1038674066@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Michiel Lange <michiel(at)minas(dot)demon(dot)nl> writes:
> I cannot create rules on views

Sure you can. Here's a trivial example. This lets a user of view
"bar" see a subset of rows in "foo", update only some fields of
those rows, and not insert or delete anything:

regression=# create table foo (f1 int, f2 text);
CREATE TABLE
regression=# create view bar as select * from foo where f1 < 100;
CREATE VIEW
regression=# create rule bar_update as on update to bar do instead
regression-# update foo set f2 = new.f2 where f1 = old.f1;
CREATE RULE
regression=# insert into foo values(1,'ok');
INSERT 547779 1
regression=# insert into foo values(101, 'u cant see me');
INSERT 547780 1
regression=# select * from bar;
f1 | f2
----+----
1 | ok
(1 row)

regression=# update bar set f1 = f1+1, f2 = f2 || ' added';
UPDATE 1
regression=# select * from bar;
f1 | f2
----+----------
1 | ok added
(1 row)

regression=# select * from foo;
f1 | f2
-----+---------------
101 | u cant see me
1 | ok added
(2 rows)

If you wanted to allow inserts or deletes via the view, you'd write
additional rules for that.

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Ewald Geschwinde 2002-11-30 17:02:06 Re: switching databases
Previous Message Heiko Kehlenbrink 2002-11-30 11:22:50 dbf2sql