views:

57

answers:

1

I don't have the (expensive) SQL standard at hand; what are updatable queries in SQL core/foundation?

I see that PostgreSQL doesn't support them, but some other databases do; can you point me to the documentation on how they work in those databases?

PostgreSQL has query rewriting and updatable views with the rule system; is this very different?

A: 

I just found this Oracle example on Wikipedia that looks like a SELECT in the left-hand side of an update:

UPDATE (
SELECT *
  FROM articles  
  JOIN classification c 
    ON a.articleID = c.articleID 
) AS a
SET a.[updated_column] = updatevalue
WHERE c.classID = 1

Whereas most databases need the query to be written as:

UPDATE a
SET a.[updated_column] = updatevalue
FROM articles a 
JOIN classification c 
ON a.articleID = c.articleID 
WHERE c.classID = 1

Links: The oracle reference docs, examples.

Do other databases support this? Where is it documented?


Having now laid my grubby mittens on the standard, I'll just quote it.

E153 refers to:

Subclause 7.12, “<query expression>”: A <query expression> is updatable even though its <where clause> contains a <subquery>

Since a query expressions is also a table (not intuitive but it's in SQL99 4.16.3 Operations involving tables), it means the query expression is an “udpatable table”. Which according to 4.16 means I can INSERT into and DELETE from them.

Which means I can run the above, as well as:

DELETE FROM (SELECT * FROM t1 JOIN t2 WHERE t1c1 = t2c3);

There are some more rules to determine what query expressions are updatable, contained in sql99-foundation 7.11 and 7.12; they are rather involved. PostgreSQL doesn't let query expressions that aren't table names be updatable. There is some work being done on updatable views. I'm not sure how useful the feature is outside of views, but the standard is definitely interesting, and weirder than expected.

[It's feeling lonely here. Picking my answer as best.]

Tobu