CS145 Lecture Notes (11) -- Views



Three-level vision of database: Why use views? => Real database applications use lots and lots (and lots and lots) of views


Defining Views

SQL Syntax

  CREATE VIEW ViewName AS Query
or to rename attributes:
  CREATE VIEW ViewName(A1, A2, ..., Am) AS Query



[[On-Line Views Demo]]

Schema for examples:
   Student(ID, name, GPA, sizeHS)       // ID is key
   Campus(location, enrollment, rank)   // location is key
   Apply(ID, location, major, decision) // (ID,location,major) is key
Topics covered:

Modifying Views

Precise conditions for views with meaningful, unambiguous modifications are very complicated: may involve keys, equality conditions, etc.

SQL2 View Modifications

Conservative conditions to guarantee meaningful and unambiguous modifications. View must be defined as:

MySQL View Modifications

Somewhat less restrictive:

Materialized Views

So far we've discussed virtual views: Materialized views: Question: Why are views not always materialized?




Example: Berkeley applicants
  CREATE MATERIALIZED VIEW Berk AS
    SELECT Student.ID, name, major
    FROM Student, Apply
    WHERE Student.ID = Apply.ID AND location = 'Berkeley';
Question: What modifications to regular relations might invalidate current contents of Berk?





Even though materialized views are stored as tables, updates to materialized views incur same ambiguity problems as updates to virtual views.

Question: Why not just update materialized view table?





Question: How does one decide whether to use a virtual or a materialized view?








Final Notes