View (SQL): Difference between revisions

Content deleted Content added
m Undid revision 799473945 by 223.255.225.67 (talk)
Rescuing 1 sources and tagging 0 as dead.) #IABot (v2.0.9.5
 
(47 intermediate revisions by 38 users not shown)
Line 1:
{{Short description|Database stored query result set}}
In [[database theory]], a '''view''' is the [[result set]] of a ''stored'' [[Query language|query]] on the [[data]], which the [[database]] users can query just as they would in a persistent database collection object. This pre-established query command is kept in the database dictionary. Unlike ordinary ''base tables'' in a [[relational database]], a view does not form part of the [[database design|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. In some [[NoSQL]] databases, views are the only way to query data.
{{More citations needed|date=December 2023}}
In a [[database theory]], a '''view''' is the [[result set]] of a ''stored'' [[Query language|query]] onthat thepresents [[data]],a limited whichperspective of the [[database]] usersto cana queryuser.<ref justname="mysql-views">{{cite asweb they| wouldurl=https://dev.mysql.com/doc/refman/8.0/en/views.html in| title=25.5 Using Views | author=<!--Not stated--> | date=2023-12-12 | website=MySQL | publisher=Oracle | access-date=2023-12-12 | quote=Views are stored queries that when invoked produce a persistentresult databaseset. collectionA objectview acts as a virtual table. | archive-url=https://web.archive.org/web/20231123070115/http://dev.mysql.com/doc/refman/8.0/en/views.html | archive-date=2023-11-23 | url-status=live }}</ref> This pre-established query command is kept in the database[[data dictionary]]. Unlike ordinary ''[[base tablestable]]s'' in a [[relational database]], a view does not form part of the [[database design|physical schema]]: as a result set, it is a virtual table<ref name="mysql-views" /> 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. In some [[NoSQL]] databases, views are the only way to query data.
 
Views can provide advantages over tables:
* Views can represent a subset of the data contained in a table. Consequently, a view can limit the degree of exposure of the underlying tables to the outer world: a given user may have permission to query the view, while denied access to the rest of the base table.<ref name="SQL-reference-groff-weinberg">{{cite book
| last1 = Groff
* Views can [[Join (SQL)|join]] and simplify multiple tables into a single virtual table.
| first1 = James R.
| last2 = Weinberg | first2 = Paul N.
| date = 1999
| title = SQL: The Complete Reference
| url = http://englishonlineclub.com/pdf/SQL%20-%20The%20Complete%20Reference%20[EnglishOnlineClub.com].pdf
| ___location = <!-- not stated -->
| publisher = Osborne/McGraw-Hill
| pages = 291–292
| isbn = 0072118458
}}</ref>
* Views can [[Join (SQL)|join]] and simplify multiple tables into a single virtual table.<ref name="SQL-reference-groff-weinberg" />
* Views can act as aggregated tables, where the [[database engine]] aggregates data ([[summation|sum]], [[average]], etc.) and presents the calculated results as part of the data.
* Views can hide the complexity of data. For example, a view could appear as Sales2000Sales2020 or Sales2001Sales2021, transparently [[Partition (database)|partitioning]] the actual underlying table.
* Views take very little space to store; the database contains only the definition of a view, not a copy of all the data that it presents.
* Views structure data in a way that classes of users find natural and intuitive.<ref name="SQL-reference-groff-weinberg"/>
* Depending on the [[SQL]] engine used, views can provide extra security.
 
Just as a [[function (computing)|function]] (in programming) can provide [[Abstraction (computer science)|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 [[Database normalization|normalization]] of databases above [[second normal form]] would become much more difficult. Views can make it easier to create lossless join decomposition.
 
Line 14 ⟶ 26:
 
== Read-only vs. updatable views ==
Database practitionersViews can definebe viewsdefined as [[file system permissions|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 (SQL)|INSERT]], [[Update (SQL)|UPDATE]], and [[Delete (SQL)|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 [[Database trigger|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==
==Advanced view features ==
{{further|Materialized view}}
Various [[database management system]]s have extended the views from read-only subsets of [[data]].
 
Various [[Oracledatabase Databasemanagement system]]s introducedhave extended the conceptviews from read-only subsets of [[data]], particularly [[materialized view]]s: pre-executed, non-virtual views commonly used in [[Data warehouse|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 DB2Db2]] 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 ==
Line 27 ⟶ 41:
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:
 
<sourcesyntaxhighlight lang="sql">
-- accounts_view:
-------------
SELECT name,
Line 39 ⟶ 53:
JOIN accounts_table a
ON a.customer_id = c.customer_id
</syntaxhighlight>
</source>
 
then the application could run a simple query such as:
 
<sourcesyntaxhighlight lang="sql">
-- Simple query
------------
SELECT name,
balance
FROM accounts_view
</syntaxhighlight>
</source>
 
The RDBMS then takes the simple query, replaces the equivalent view, then sends the following to the [[query optimizer]]:
 
<sourcesyntaxhighlight lang="sql">
-- Preprocessed query:
------------------
SELECT name,
Line 66 ⟶ 80:
FROM table_customers c JOIN accounts_table a
ON a.customer_id = c.customer_id )
</syntaxhighlight>
</source>
 
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 ==
* [[Bidirectionalization]]
 
== External links ==
* [http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db2z10.doc.intro/src/tpc/db2z_typesoftables.htm Materialized query tables in DB2]
* [http://msdn.microsoft.com/en-us/library/ms187956.aspx Views in Microsoft SQL Server]
* [http://dev.mysql.com/doc/refman/5.7/en/views.html Views in MySQL]
* [http://www.postgresql.org/docs/current/interactive/tutorial-views.html Views in PostgreSQL]
* [http://www.sqlite.org/lang_createview.html Views in SQLite]
* [http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_8004.htm#SQLRF01504 Views in Oracle 11.2]
* [http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views Views in CouchDB]
* [http://doc.nuodb.com/display/doc/CREATE+VIEW Views in NuoDB]
* [https://web.archive.org/web/20110303073052/http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_6002.htm#SQLRF01302 Materialized Views in Oracle 11.2]
 
{{Databases}}
 
==References==
{{Reflist}}
 
{{DEFAULTSORT:View (Database)}}