Re: Setting up updatable views ?
Date: Fri, 05 Oct 2007 09:13:22 -0700
>From SQL FOR SMARTIES:
18.02. Updatable and Read-Only VIEWs
Unlike base tables, VIEWs are either updatable or read-only, but not both. INSERT, UPDATE, and DELETE operations are allowed on updatable VIEWs and base tables, subject to any other constraints. INSERT, UPDATE, and DELETE are not allowed on read-only VIEWs, but you can change their base tables, as you would expect.
An updatable VIEW is one that can have each of its rows associated with exactly one row in an underlying base table. When the VIEW is changed, the changes pass through the VIEW to that underlying base table unambiguously. Updatable VIEWs in Standard SQL are defined only for queries that meet these criteria
- They are built on only one table,
- No GROUP BY clause
- No HAVING clause
- No aggregate functions
- No calculated columns
- No UNION, INTERSECT or EXCEPT
- No SELECT DISTINCT clause
- Any columns excluded from the VIEW must be NULL-able or have a DEFAULT in the base table, so that a whole row can be constructed for insertion.
By implication, the VIEW must also contain a key of the table. In short, we are absolutely sure that each row in the VIEW maps back to one and only one row in the base table.
Some updating is handled by the CASCADE option in the referential integrity constraints on the base tables, not by the VIEW declaration.
The definition of updatability in Standard SQL is actually pretty limited, but very safe. The database system could look at information it has in the referential integrity constraints to widen the set of allowed updatable VIEWs. You will find that some implementations are now doing just that, but it is not common yet. The SQL standard definition of an updatable VIEW is actually a subset of the possible updatable VIEWs, and a very small subset at that. The major advantage of this definition is that it is based on syntax and not semantics. For example, these VIEWs are logically identical:
CREATE VIEW Foo1 -- updatable, has a key!
AS SELECT *
WHERE x IN (1,2);
CREATE VIEW Foo2 -- not updateable!
AS SELECT *
WHERE x = 1
WHERE x = 2;
But Foo1 is updateable and Foo2 is not. While I know of no formal proof, I suspect that determining if a complex query resolves to an updatable query for allowed sets of data values possible in the table is an NP-complete problem.
Without going into details, here is a list of types of queries that can yield updatable VIEWs, as taken from "VIEW Update Is Practical", (Goodman 1990):
- Projection from a single table (Standard SQL)
- Restriction/projection from a single table (Standard SQL)
- UNION VIEWs
- Set difference VIEWs
- One-to-one joins
- One-to-one outer joins
- One-to-many joins
- One-to-many outer joins
- Many-to-many joins
- Translated and coded fields
The CREATE TRIGGER mechanism for tables an action to be performed BEFORE, AFTER, or INSTEAD OF a regular INSERT, UPDATE, or DELETE to that table. It is possible for a user to write INSTEAD OF triggers on VIEWs, which catch the changes and route them to the base tables that make up the VIEW. The database designer has complete control over the way VIEWs are handled. Received on Fri Oct 05 2007 - 18:13:22 CEST