Views



index
Disabled back button Next Section
printable version

Section 0: Module Objectives or Competencies
Course Objective or Competency Module Objectives or Competency
The student will be able to explain when and how a VIEW may be needed to reflect the results of a query, and will be able to create and use VIEWs. The student will be able to explain what a VIEW is and how it can be used.
The student will be able to list and explain the reasons for developing VIEWs.
The student will be able to explain what a VIEW can be composed of.
The student will be able to create a VIEW.
The student will be able to create dynamic VIEWs.
Section 1: Overview

All SQL queries result in the creation of a temporary table to hold the results.

The VIEW command can be used to create a logical (virtual) table.

The VIEW command is especially useful because not only can it be used as a mechanism for saving queries, but it may be used to restrict user access to selected portions of the data contained in a table.

In addition, restricting access to columns and rows allows users to focus on the specific data that interests them.

Section 2: Syntax

CREATE VIEW viewName AS SELECT query

Example:

CREATE VIEW PRICEGT50 AS
  SELECT P_DESCRIPT, P_QOH, P_PRICE
  FROM PRODUCT
  WHERE P_PRICE > 50.00;

SELECT * FROM PRICEGT50;

Results of CREATE VIEW.
Section 3: Uses

There are at least seven primary uses of VIEWS:

  1. to save queries
  2. to provide row and column level 'security'
  3. to ensure efficient access paths
  4. to mask complexity from the user
  5. to ensure proper data derivation
  6. to rename columns
  7. to provide solutions that cannot be accomplished without views (i.e., complex queries built on the results of other queries/views)

It is useful to provide the users with a VIEW rather than the real table, because if they have access only to a view, they cannot corrupt or damage the real table.

Views can also be used as the basis for reports. For example, if you need a report that shows a summary of total product cost and quantity-on-hand statistics grouped by vendor, you could use the following:

CREATE VIEW PROD_STATS AS
  SELECT V_CODE, SUM(P_QOH*P_PRICE) AS TOTCOST, MAX(P_QOH) AS MAXQTY,
    MIN(P_QOH) AS MINQTY, AVG(P_QOH) AS AVGQTY
  FROM PRODUCT
  GROUP BY V_CODE;

SELECT * FROM PROD_STATS;

Results of another VIEW.
Section 4: Composition

VIEWS can consist of:

Section 5: Saving Queries

If you want to re-issue a query and get the same result set, the query can be saved as a text file, opened, and executed again.

SQL provides a better way to save queries in the database catalog through a view.

A view is a named query stored in the database catalog that allows it to be referred to later.

You can save a query as a view with a name like "Q6a" or "Q7", as in the following example:

CREATE VIEW Q00 AS
  SELECT P_CODE, P_DESCRIPT, P_PRICE
  FROM PRODUCT
  WHERE P_PRICE = (SELECT MAX( P_PRICE ) FROM PRODUCT);

Then to run your query and see the results, apply a SELECT statement to the view:

SELECT * FROM Q00;

Note that in MySQL you cannot save a prepared statement as a view, but it is possible to save prepared statements (or multi-statement queries) in a stored procedure by enclosing all statements inside a BEGIN-END block.

Section 6: Updatable Views

Views can be read-only or designed to allow dynamic updates to the underlying table, so if the data in the original table is altered, those alterations are reflected in the VIEWs.

However, in order to create an updatable view using MySQL, the SELECT statement that defines View must follow these restrictions (link):

Section 7: Links