View (SQL) explained

In a database, a view is the result set of a stored query that presents a limited perspective of the database to a user.[1] This pre-established query command is kept in the data dictionary. Unlike ordinary base tables in a relational database, a view does not form part of the physical schema: as a result set, it is a virtual table computed or collated dynamically from data in the database when access to that view is requested. Changes applied to the data in a relevant underlying table are reflected in the data shown in subsequent invocations of the view.

Views can provide advantages over tables:

Just as a function (in programming) can provide abstraction, so can a database view. In another parallel with functions, database users can manipulate nested views, thus one view can aggregate data from other views. Without the use of views, the normalization of databases above second normal form would become much more difficult. Views can make it easier to create lossless join decomposition.

Just as rows in a base table lack any defined ordering, rows available through a view do not appear with any default sorting. A view is a relational table, and the relational model defines a table as a set of rows. Since sets are not ordered - by definition - neither are the rows of a view. Therefore, an ORDER BY clause in the view definition is meaningless; the SQL standard does not allow an ORDER BY clause in the subquery of a CREATE VIEW command, just as it is refused in a CREATE TABLE statement. However, sorted data can be obtained from a view, in the same way as any other table - as part of a query statement on that view. Nevertheless, some DBMS (such as Oracle Database) do not abide by this SQL standard restriction.

Read-only vs. updatable views

Views can be defined as read-only or updatable. If the database system can determine the reverse mapping from the view schema to the schema of the underlying base tables, then the view is updatable. INSERT, UPDATE, and DELETE operations can be performed on updatable views. Read-only views do not support such operations because the DBMS cannot map the changes to the underlying base tables. A view update is done by key preservation.

Some systems support the definition of INSTEAD OF triggers on views. This technique allows the definition of other logic for execution in place of an insert, update, or delete operation on the views. Thus database systems can implement data modifications based on read-only views. However, an INSTEAD OF trigger does not change the read-only or updatable property of the view itself.

Materialized views

Various database management systems have extended the views from read-only subsets of data, particularly materialized views: pre-executed, non-virtual views commonly used in data warehousing. They give a static snapshot of the data and may include data from remote sources. The accuracy of a materialized view depends on the frequency of trigger mechanisms behind its updates.

Materialized views were introduced by Oracle Database, while IBM Db2 provides so-called "materialized query tables" (MQTs) for the same purpose. Microsoft SQL Server introduced in its 2000 version indexed views which only store a separate index from the table, but not the entire data. PostgreSQL implemented materialized views in its 9.3 release.

Equivalence

A view is equivalent to its source query. When queries are run against views, the query is modified. For example, if there exists a view named accounts_view with the content as follows:

-- accounts_view:-------------SELECT name, money_received, money_sent, (money_received - money_sent) AS balance, address, ... FROM table_customers c JOIN accounts_table a ON a.customer_id = c.customer_id

then the application could run a simple query such as:

-- Simple query------------SELECT name, balance FROM accounts_view

The RDBMS then takes the simple query, replaces the equivalent view, then sends the following to the query optimizer:

-- Preprocessed query:------------------SELECT name, balance FROM (SELECT name, money_received, money_sent, (money_received - money_sent) AS balance, address, ... FROM table_customers c JOIN accounts_table a ON a.customer_id = c.customer_id)

The optimizer then removes unnecessary fields and complexity (for example it is not necessary to read the address, since the parent invocation does not make use of it) and then sends the query to the SQL engine for processing.

See also

Notes and References

  1. Web site: 25.5 Using Views . . 2023-12-12 . MySQL . Oracle . 2023-12-12 . Views are stored queries that when invoked produce a result set. A view acts as a virtual table.. http://web.archive.org/web/20231123070115/http://dev.mysql.com:80/doc/refman/8.0/en/views.html . 2023-11-23 . live .
  2. Book: Groff . James R. . Weinberg . Paul N. . 1999 . http://englishonlineclub.com/pdf/SQL%20-%20The%20Complete%20Reference%20[EnglishOnlineClub.com.pdf SQL: The Complete Reference ]. . Osborne/McGraw-Hill . 291–292 . 0072118458.