Database view
A database view is a named query stored in a database that behaves like a table. It holds the rule for selecting and combining data from underlying tables, so the same logic can be reused without rewriting the query.
What it is
A database view is a saved query that is given a name and used like a table. When a user reads from the view, the database runs the stored query against the underlying tables and returns the result. A plain view holds no data of its own.
A view is made of:
- A name, used in other queries in place of a table name.
- A defining query, which selects columns, filters rows, joins tables or aggregates.
- The underlying tables, called base tables, or other views.
The ISO/IEC 9075 family of standards specifies SQL, the language in which relational views are defined. Part 2 sets out the data structures and basic operations on SQL data, and Part 1 gives the conceptual framework the other parts use.
Two kinds are common. A standard view is evaluated each time it is read, so it always reflects the base tables. A materialised view stores the result of its query and is refreshed on a schedule or on request, which makes reading faster but leaves the stored result behind the base tables between refreshes.
Why it matters
A reporting query often repeats the same joins and filters, for example the rule that defines an active customer or a booked invoice. Written separately in each report, the rule drifts and two reports give different answers.
A view holds the rule in one place. Changing the definition changes every report that reads it. A view can also expose only chosen columns and rows, so analysts see what they need without access to the full base table.
Worked example
Illustrative numbers. An invoice table has 2,000 rows with invoice number, customer, status, month and amount. Reports need only invoices with status “booked” and a monthly total.
An analyst defines a view called booked_invoices that returns the invoices with status “booked”. Three reports read from booked_invoices. Later the definition of booked changes to exclude a second status, and the analyst edits the view once. All three reports then use the changed rule, with no change to the reports themselves.
If the rule had been copied into each report, three edits would be needed and one might be missed.
How to read it
The definition of the view is the documentation of the rule. Reading it shows which tables feed the view, which filters apply and how rows are combined.
A materialised view should be read together with its refresh time, because its figures are as of that moment.
Assumptions
- The base tables keep the columns and meaning that the defining query relies on.
- Users of the view know its definition, or find it in a data dictionary.
- A materialised view is refreshed at a frequency that suits the reports that read it.
Who uses it
- Data analysts build views for repeated joins and filters and read from views written by others.
- Business intelligence analysts connect dashboards to views, so that every chart uses the same definition.
- Regulatory reporting analysts use views to fix the extraction logic behind a filing and to rerun it.
- FP&A analysts pull actuals from a view that applies the agreed account mapping.
Common confusions
- A standard view is not a copy of the data. It is a stored query, so it holds no rows of its own.
- A view is not the same as a table. Many views cannot be updated directly, particularly those that join tables or aggregate.
- A materialised view can be out of date. It shows the data as of its last refresh.
- A view is not a security control on its own. Access to a view and access to its base tables are granted separately.