Re: question on update/delete rules on views

From: Kyle Bateman <kyle(at)actarg(dot)com>
To: Brook Milligan <brook(at)biology(dot)nmsu(dot)edu>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: question on update/delete rules on views
Date: 2000-05-17 20:47:02
Message-ID: 39230546.62BE1AD9@actarg.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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;

Attachment Content-Type Size
kyle.vcf text/x-vcard 291 bytes

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Brook Milligan 2000-05-17 21:01:35 Re: question on update/delete rules on views
Previous Message Richard Huxton 2000-05-17 19:45:32 Re: question on update/delete rules on views