Re: Thoughts about updateable views - Mailing list pgsql-hackers

From Richard Huxton
Subject Re: Thoughts about updateable views
Date
Msg-id 41C94154.3050708@archonet.com
Whole thread Raw
In response to Re: Thoughts about updateable views  (Jaime Casanova <systemguards@yahoo.com>)
Responses Re: Thoughts about updateable views
Re: Thoughts about updateable views
List pgsql-hackers
Jaime Casanova wrote:
> In a galaxy far, far away Bernd wrote:
> 
> The context:
> http://archives.postgresql.org/pgsql-hackers/2004-03/msg00999.php
> 
> 
>>so joined views are even not updateable, too.
> 
> 
> I don't find the why of this on the specs and the 
> threads about this issue ignore the comment.

BEGIN QUOTE
In O'Reilly there are also the conditions mentioned a view has to pass 
when declaring it updateable (also SQL99):


- The SELECT of the CREATE VIEW command depends only on one table
- UNION, EXCEPT and INTERSECT are not part of the View's SELECT
- No GROUP BY or HAVING
- Pseudo-Columns are forbidden, too (but i think that can improved to 
functions....)
- No Aggregates
- No DISTINCT

so joined views are even not updateable, too.
END QUOTE

Well, I haven't checked the specs, but this list is clearly incorrect 
from a theoretical point of view (I've been thinking about this recently).

There are two things (AFAICT) you need to be able to do to update (NOTE 
- not insert) a view. 1. Identify the underlying table(s) for the updated column(s) 2. Identify (primary) key values
forthe table(s) being updated.
 
So - I could have a join listing users and how many email aliases they 
have (so sum()) and still update their name, so long as the key for the 
users table was present in the view.

Any summarising rules out updates for the summarised tables (because you 
no longer have a key available). Similarly, expression/function columns 
can't be updated (except in certain special cases).

UNION etc doesn't necessarily mean you can't update, so long as the 
underlying table/key can be identified.

For INSERTing to a view, the same rules apply, but obviously you need to  be able to identify table/keys for all
columnsin the view. This 
 
clearly rules out aggregates etc.

--  Richard Huxton  Archonet Ltd


pgsql-hackers by date:

Previous
From: Hannu Krosing
Date:
Subject: Re: getting 'order by' working with unicode locale? ICU?
Next
From: "Dave Hartwig"
Date:
Subject: Can't Restart ver 8.0b3