Views allow multiple users to see different presentations of the same data. For example, several users may be operating on a table of data about employees. One may see data about some employees but not others; another may see data about all employees but none of their salaries; and a third may see data about employees joined together with some data from another table. Each of these users is operating on a view that is derived from the real table of data about employees. Each view appears to be a table and has a name of its own.
You can create views with authorization statements to control access to sensitive data. For example, you might create a view based on a GROUP BY query that gives certain users access to the average salary of employees in each department, but prevents them from seeing any individual salaries.
A view is a dynamic "window" on tables. When you update a real table, you can see the updates through a view; when you update a view, the real table underlying the view is updated. There are, however, restrictions on modifying tables through a view.
Because a view is not physically stored, you cannot create an index on it. However, if you create an index on the real table underlying a view, you may improve the performance of queries on the view.
>>-CREATE VIEW--view_name----+----------------------------+-----> | .-,--------------. | | V | | '-(-----column_name---+---)--' >----AS--subselect--+-------------------+---------------------->< '-WITH CHECK OPTION-' |
In the following example, a view is created from the EMPLOYEE table:
CREATE VIEW PHONEBOOK (FNAME, LNAME, NUMBER, DEPART, JOBTITLE) AS SELECT FIRSTNME, LASTNAME, PHONENO, WORKDEPT, JOB FROM EMPLOYEE WHERE JOB <> 'PRES' WITH CHECK OPTION
The CREATE VIEW statement causes the indicated select-statement to be stored as the definition of a new view, and gives a name to the view and (optionally) to each column in it. If you do not specify the column names, the columns of the view inherit the names of the columns from which they are derived.
You must specify a name for any view column that is not derived directly from a single table column (for example, if a view column is defined as AVG(SALARY) or SALARY+COMMISSION).
Columns derived in this manner are often called virtual columns, (and contain virtual data). You must also specify new column names if the selected columns of the view do not have unique names (for example, if the view is a join of two tables, each of which has a column named PROJNO).
In general, the data types of the columns of the view are inherited from the columns on which they are defined. If a view column is defined on a function, the data type of the view column will be the data type of the function result. (For more details on functions, refer to the DB2 Server for VSE & VM SQL Reference manual.)
If you want to prevent the execution of subsequent inserts or updates to the view that involve data that is outside the domain of the view's definition (as specified in the WHERE clause of its subselect), you can add the WITH CHECK OPTION clause. This clause, however, is not allowed for updateable views that are built on subqueries. The checking that is performed at insert or update time is performed according to a set of rules that cover the situation in which a view is dependent on other views. See the DB2 Server for VSE & VM SQL Reference manual for these rules.
Some other considerations when creating views are:
You can write queries (select-statements) against views exactly as if they were real tables. When you make a query against a view, the query is combined with the definition of the view to produce a new query against real stored tables. This query is then processed in the usual way. For example, the following query might be written against the view PHONEBOOK that was defined under Creating a View:
SELECT FNAME,LNAME FROM PHONEBOOK WHERE DEPART = 'D11' ORDER BY 2
The system combines the query with the definition of PHONEBOOK, and processes the resulting internal query:
SELECT FIRSTNME, LASTNAME FROM EMPLOYEE WHERE JOB <> 'PRES' AND WORKDEPT = 'D11' ORDER BY 2
During the processing of a query on a view, the system may detect and report errors (by a negative SQLCODE) in either of two phases:
Note: | If a view materialization is required to process the view, this view must not contain any LONG VARCHAR columns in the view definition. For a detailed description of view materialization, refer to the DB2 Server for VSE & VM Database Administration manual. |
Like select-statements, INSERT, DELETE, and UPDATE statements can be applied to a view just as though it were a real stored table. The SQL statement that operates on the view is combined with the definition of the view to form a new SQL statement that operates on a stored table. Any data modification made by such a statement is visible to users of the view, the underlying table, or other views defined on the same table (if the views "overlap" in the modified area).
The following is an example of an update applied to the view PHONEBOOK,
showing how the update can be modified to operate on the real table
EMPLOYEE:
View Definition for PHONEBOOK: |
CREATE VIEW PHONEBOOK (FNAME, LNAME, NUMBER, DEPART, JOBTITLE) AS SELECT FIRSTNME, LASTNAME, PHONENO, WORKDEPT, JOB FROM EMPLOYEE WHERE JOB <> 'PRES' WITH CHECK OPTION |
UPDATE PHONEBOOK SET NUMBER = '9111' WHERE LNAME = 'SMITH' AND FNAME = 'DANIEL'
becomes:
UPDATE EMPLOYEE SET PHONENO = '9111' WHERE LASTNAME = 'SMITH' AND FIRSTNME = 'DANIEL' AND JOB <> 'PRES'
Note: | Because of the WITH CHECK OPTION, the following update will not be allowed
when Sally takes over as president:
UPDATE PHONEBOOK SET JOBTITLE = 'PRES' WHERE LNAME = 'KWAN' AND FNAME = 'SALLY' |
You must observe the following rules when modifying tables through a view:
You can use an INSERT statement on a view that does not contain all the columns of the stored table on which it is based. For example, consider the EMPLOYEE table with none of the columns defined as NOT NULL. You could insert rows into the view PHONEBOOK even though it does not contain the MIDINIT, EDLEVEL or any other columns of the underlying table EMPLOYEE.
You can insert or update rows of a view in such a way that they do not satisfy the definition of the view. For example, the view PHONEBOOK is defined by the condition JOB <> 'PRES'. It would be possible to insert rows into PHONEBOOK having a value equal to 'PRES' in the JOB column. This insertion takes effect on the underlying table, EMPLOYEE, but the resulting rows are not visible in the view PHONEBOOK, because they do not satisfy the definition of PHONEBOOK. In fact, an update to PHONEBOOK that sets JOB='PRES' causes a row to "vanish" from PHONEBOOK (a cursor positioned on the row retains its position, but later scans through PHONEBOOK do not see this row). If you want to ensure that all rows inserted or updated are subsequently visible in the view, then define your view with 'WITH CHECK OPTION'.
However, the EMPLOYEE table does have columns defined as NOT NULL, and two of them (MIDINIT and EDLEVEL) are not available through the PHONE view. If you try to insert a row through the view, the system attempts to insert NULL values into all the EMPLOYEE columns that are "invisible" through the view. Because the MIDINIT and the EDLEVEL columns are not included in the view, and do not permit null values, the system does not permit the insertion through the view.
Be extremely careful when updating tables through views that may contain duplicate rows. For example, suppose a view JOBS is defined on the EMPLOYEE table containing only the columns WORKDEPT and JOB. Because EMPNO is not included in the view, and many employees may have the same job description, a user of the view cannot tell which EMPNO corresponds to a given row of the view. If the user positions a cursor on a row where JOB = 'CLERK', and then updates the current row of this cursor, a row of the stored EMPLOYEE table is updated. However, because there may be many clerks in the EMPLOYEE table, and the unique qualifier EMPNO is not part of the view, the user cannot control which employee is updated.
Format
>>-DROP VIEW--view_name---------------------------------------->< |
The DROP VIEW statement drops the definition of the indicated view from the database. When you drop a view, the system also:
The invalid packages remain in the database until they are explicitly dropped by a DROP PACKAGE statement. When an invalid package is next invoked, the system attempts to regenerate it and restore its validity. However, if the program contains any SQL statement that refers to a dbspace, table, or view that has been dropped, that SQL statement returns an error code at run time.
If a DROP VIEW statement attempts to drop a view that is currently in use by another running logical unit of work, the statement is queued until that LUW ends.