3.8

Overview of Views in SQL

  • A view is a virtual table based on the result set of a SELECT statement.

  • The CREATE VIEW statement is used to create a view table:

    • Specifies the view name.

    • The view query defines the data within the view.

    • Can optionally specify column names; if not specified, they are the same as in the result of the view query.

View Table Characteristics

  • View data is not stored; it appears dynamically in SQL statements.

  • A materialized view:

    • Stores its data permanently.

    • Must be refreshed whenever the underlying base tables change.

  • Base tables serve as the original source tables and are created as tables (not as views).

Benefits of Using Views

  • Views save complex or optimized queries for improved database performance.

  • The performance of a user’s query on a view matches that of the corresponding direct query on base tables.

  • Views can reference other views.

  • Views can hide specific rows and columns from users for simplified access.

Limitations and Constraints on Views

Manipulating Data in Views

  • Inserting and updating records can be problematic:

    • A primary key in the base table that does not appear in a view can create issues; inserting into the view might generate a null primary key value, which is not allowed due to primary key constraints.

    • Aggregated values on the view cannot be easily converted into base table values, thus restricting updates or inserts involving aggregation.

    • Join views may cause additional complications, as the effect of deletion in join views is undefined and therefore not allowed.

Constraints with WITH CHECK Clause

  • The WITH CHECK option rejects inserts and updates that do not satisfy the view query's WHERE clause.

  • When a violation occurs, the database generates an error message indicating the restriction or violation.