Download In This Chapter

Transcript
Modifying with a View
Modifying with a View
You can modify views as if they were tables. Some views can be modified and
others not, depending on their SELECT statements. The restrictions are
different, depending on whether you use DELETE, UPDATE, or INSERT
statements.
A view is modifiable if the SELECT statement that defined it did not contain
any of the following items:
■
A join of two or more tables
■
An aggregate function or the GROUP BY clause
■
The DISTINCT keyword or its synonym UNIQUE
■
The UNION keyword
When a view avoids all these restricted features, each row of the view corresponds to exactly one row of one table.
Deleting with a View
You can use a DELETE statement on a modifiable view as if it were a table. The
database server deletes the proper row of the underlying table.
Updating a View
You can use an UPDATE statement on a modifiable view. However, the
database server does not support updating any derived column. A derived
column is a column produced by an expression in the select list of the CREATE
VIEW statement (for example, order_date + 30).
The following example shows a modifiable view that contains a derived
column and an UPDATE statement that can be accepted against it:
CREATE VIEW response(user_id, received, resolved, duration) AS
SELECT user_id,call_dtime,res_dtime, res_dtime-call_dtime
FROM cust_calls
WHERE user_id = USER;
UPDATE response SET resolved = TODAY
WHERE resolved IS NULL;
Granting and Limiting Access to Your Database 6-31