pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kyle Bateman <kyle@actarg.com>
To: Brook Milligan <brook@biology.nmsu.edu>
Cc: pgsql-sql@postgresql.org
Subject: Re: question on update/delete rules on views
Date: Wed, 17 May 2000 14:47:02 -0600
Message-ID: <39230546.62BE1AD9@actarg.com> (raw)
References: <3921E506.82424CAF@actarg.com>
	<200005171352.HAA19867@biology.nmsu.edu>
	<3922B7A2.5BEF5F5E@actarg.com>
	<200005171751.LAA20341@biology.nmsu.edu>

Brook Milligan wrote:

>    >    create rule view_a_r_update as on update to view_a
>    >            do instead
>    >                    update a set two = new.two
>    >                    where id = old.id;
>    >
>    > where id is a primary key in your table.
>
> I think you misunderstand what is going on.  The original WHERE clause
> (in your query) defines a set of tuples to which the UPDATE rule will
> be applied.  In the example above, each of those tuples will have a
> primary key value (old.id in that case) and the matching field(s) in
> table (or view) a will be changed as dictated by the rule.  Thus, for
> every tuple selected by your WHERE clause, the corresponding tuple in
> the underlying table will be updated.  Note that as many fields as you
> wish to allow updates on can be included in the set ... part of the
> rule; any that are not different will just be changed to the same
> value (i.e., there will be no effect).  Consequently, you don't need
> lots of rules for every combination of columns (unless there are other
> reasons to restrict the set of columns modifiable by different views).
>

Thanks Brook.  That makes sense to me now.  Here's the test file that
demonstrates the proper (or at least a better) approach.  I'll post the whole
thing for the benefit of others (who wish to avoid looking as stupid as me).
Even with all the examples of rules and views in the docs, I had a hard time
finding an example of a view that simply gives fully functional access to a
single underlying table.

BTW, do you have an equally sensible explanation of how the "where condition"
that is part of the rule syntax differs from the where clause that comes after
the "do instead"?

drop table a;

create table a (
one char(2),
two char(2),
three int4,
primary key (one,two)
);

insert into a values ('aa','xz', 10);
insert into a values ('ab','xz', 12);
insert into a values ('ac','xz', 100);
insert into a values ('ad','xz', 11);
insert into a values ('ae','xz', 15);
insert into a values ('ai','xz', 30);


drop view view_a;
create view view_a as select one, two, three from a;

create rule view_a_r_insert as on insert to view_a
    do instead
        insert into a (one, two, three)
        values (new.one, new.two, 100);


insert into view_a (one, two) values ('az','xy');

create rule view_a_r_update as on update to view_a
    do instead
        update a set one = new.one, two = new.two, three = new.three
        where one = old.one and two = old.two;

select * from a;
update view_a set two = 'mn' where one = 'az';
select * from a;
update view_a set two = 'mo' where three = 100;
select * from a;

Attachments:

kyle.vcf (text/x-vcard, 290B) download begin:vcard n:Bateman;Kyle tel;fax:801-377-8096 tel;work:801-377-8033x101 x-mozilla-html:FALSE url:www.actiontarget.com org:Action Target Inc adr:;;PO Box 636;Provo;UT;84603;US version:2.1 email;internet:kyle@actiontarget.com title:President x-mozilla-cpt:;-15520 fn:Kyle Bateman end:vcard

view thread (8+ messages)  latest in thread

Message-ID: <39230546.62BE1AD9@actarg.com>
Permalink:  ../39230546.62BE1AD9@actarg.com/
Also on:    postgresql.org/message-id/39230546.62BE1AD9@actarg.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: kyle@actarg.com, brook@biology.nmsu.edu
  Subject: Re: question on update/delete rules on views
  In-Reply-To: <39230546.62BE1AD9@actarg.com>
  References: <3921E506.82424CAF@actarg.com> <200005171352.HAA19867@biology.nmsu.edu> <3922B7A2.5BEF5F5E@actarg.com> <200005171751.LAA20341@biology.nmsu.edu> <39230546.62BE1AD9@actarg.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox