Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

Retrieving data through views [Table of Contents] Dropping views

Transact-SQL User's Guide

[-] Chapter 9 Views: Limiting Access to Data
[-] Modifying data through views

Modifying data through views

Although Adaptive Server places no restrictions on retrieving data through views, and although Transact-SQL places fewer restrictions on modifying data through views than other versions of SQL, the following rules apply to various data modification operations:

When you attempt an update, insert, or delete for a view, Adaptive Server checks to make sure that none of the above restrictions is violated and that no data integrity rules are violated.

Restrictions on updating views

Restrictions on updated views apply to these areas:

Computed columns in a view definition

This restriction applies to columns of views that are derived from computed columns or built-in functions. For example, the amt_due column in the view accounts is a computed column.

create view accounts (title_id, advance, amt_due) 
as select titles.title_id, advance, 
(price * royalty/100) * total_sales 
from titles, roysched 
where price > $15 
  and advance > $5000 
and titles.title_id = roysched.title_id 
and total_sales between lorange and hirange 

The rows visible through accounts are:

select * from accounts
title_id     advance     amt_due 
--------     --------    --------- 
PC1035       7,000.00    32,240.16 
PC8888       8,000.00     8,190.00 
PS1372       7,000.00       809.63 
TC3218       7,000.00       785.63 
 
(4 rows affected) 

updates and inserts to the amt_due column are not allowed because there is no way to deduce the underlying values for price, royalty, or year-to-date sales from any value you might enter in the amt_due column. delete operations do not make sense because there is no underlying value to delete.

group by or compute in a view definition

This restriction applies to all columns in views that contain aggregate values--that is, views whose definition includes a group by or compute clause. Here is a view defined with a group by clause and the rows seen through it:

create view categories (category, average_price) 
as select type, avg(price) 
from titles 
group by type 
select * from categories 
category         average_price 
-------------    ------------- 
UNDECIDED                 NULL 
business                 13.73 
mod_cook                 11.49 
popular_comp             21.48 
psychology               13.50 
trad_cook                15.96 
 
(6 rows affected) 

It does not make sense to insert rows into the view categories. To what group of underlying rows would an inserted row belong? Updates on the average_price column are not allowed because there is no way to know from any value you might enter there how the underlying prices should be changed.

NULL values in underlying objects

This restriction applies to insert statements when some NOT NULL columns are contained in the tables or views from which the view is derived.

For example, suppose null values are not allowed in a column of a table that underlies a view. Normally, when you insert new rows through a view, any columns in underlying tables that are not included in the view are given null values. If null values are not allowed in one or more of these columns, no inserts can be allowed through the view.

For example, in this view:

create view business_titles 
as select title_id, price, total_sales 
from titles 
where type = "business" 

Null values are not allowed in the title column of the underlying table titles, so no insert statements can be allowed through business_view. Although the title column does not even exist in the view, its prohibition of null values makes any inserts into the view illegal.

Similarly, if the title_id column has a unique index, updates or inserts that would duplicate any values in the underlying table are rejected, even if the entry does not duplicate any value in the view.

Views created using with check option

This restriction determines what types of modifications you can make through views with check options. If a view has a with check option clause, each row inserted or updated through the view must be visible within the view. This is true whether you insert or update the view directly or indirectly, through another derived view.

Multitable views

This restriction determines what types of modifications you can make through views that join columns from multiple tables. Adaptive Server prohibits delete statements on multitable views, but allows update and insert statements that would not be allowed in other systems.

You can insert or update a multitable view if:

For example, consider the following view, which includes columns from both titles and publishers and has no with check option clause:

create view multitable_view
as select title, type, titles.pub_id, state
from titles, publishers
where titles.pub_id = publishers.pub_id

A single insert or update statement can specify values either for the columns from titles or for the column from publishers:

update multitable_view
set type = "user_friendly"
where type = "popular_comp"

However, this statement fails because it affects columns from both titles and publishers:

update multitable_view
set type = "cooking_trad",
state = "WA"
where type = "trad_cook"

Views with IDENTITY columns

This restriction determines what types of modifications you can make to views that include IDENTITY columns. By definition, IDENTITY columns are not updatable. Updates through a view cannot specify an IDENTITY column value.

Inserts to IDENTITY columns are restricted to:

To enable such inserts through a view, use set identity_insert on for the column's base table. It is not sufficient to use set identity_insert on for the view through which you are inserting.


Retrieving data through views [Table of Contents] Dropping views