Database Systems · SQL Aggregation and Views
Views: Virtual Tables and CREATE VIEW
Overview
A view is a virtual table produced from the result set of a SQL statement rather than stored as a fixed physical table. It matters because a statically created table becomes stale when the underlying data changes, while a view is regenerated from its query every time it is used.
Key concepts
- View: a dynamic mechanism for producing a table whenever the result is needed; a virtual table based on the result set of a SQL statement.
- Dynamic creation of content: the view is not created once and stored permanently without change. Each time it is used, the underlying SQL statement is executed again and a new result table is prepared.
- Problem it solves: creating a separate table statically leaves it unchanged, so later changes in the underlying data are not reflected in that old table. A view avoids this by producing the table fresh on demand.
- Repeated reports: when the same kind of report/table is needed repeatedly (for example, by a HOD), a view supplies it dynamically instead of relying on a one-time physical table.
- CREATE VIEW: the statement used instead of
CREATE TABLEto define a view; the view name is given and then theSELECTstatement is written as for an ordinary query. - DROP VIEW: removes a view that is no longer needed.
- Multiple views: any number of views can be created.
How it works
- Define the view with
CREATE VIEW, giving the view name and theSELECTstatement. - The view obtains its table from that SQL statement.
- The view is not saved like a static table.
- Every time the view is used, the underlying SQL statement is executed again and a new result table is prepared.
- When the view is no longer needed, remove it with
DROP VIEW.
Formulas
CREATE VIEW view_name AS
SELECT ...;
view_name: the name given to the view.SELECT ...: the query whose result set forms the virtual table.
DROP VIEW view_name;
view_name: the view to be removed.
Comparison
| Item | Static table | View |
|---|---|---|
| Storage of result | Created and kept statically | Not saved statically as a fixed result |
| Freshness of data | Does not automatically reflect later changes | Executed again whenever needed, so a fresh table is prepared |
| Nature | Physical stored table | Virtual table based on the result set of a SQL statement |
Revision summary
- A view is a virtual table based on the result set of a SQL statement — not a permanently stored result.
- Use
CREATE VIEWinstead ofCREATE TABLEwhen the requirement is a view. - The view is not created once and stored unchanged; its query runs again on every use.
- A static table does not reflect later changes in the underlying data; a view does, because it is rebuilt each time.
- Any number of views can be created.
- Remove an unneeded view with
DROP VIEW view_name;.
Previous-year exam insights
- A view is a virtual table / window into a portion of the database; it is not a stored procedure and not a trigger (it is not executed on an event).
Asked inUGC NET 2017UGC NET 2005ISRO 2007 - Views support access control/security: a user can be granted access to a view that exposes only a subset of rows/columns, without access to the base tables.
Asked inUGC NET 2016 - A view is updatable only under restrictions: it must be defined on a single base table, contain no DISTINCT, GROUP BY, HAVING, aggregate functions, or set operations, and include the primary key (no NULL-producing columns); otherwise updates through it are not allowed.
- WITH CHECK OPTION on a view prevents inserts/updates through the view that would produce rows not satisfying the view's WHERE condition.
- A view is not physically stored with data (only its definition is stored in the data dictionary); therefore it costs no extra storage but its query is re-evaluated on each reference, which can be slower than a base table.
- A materialized view, unlike an ordinary view, does store the computed result and must be refreshed to reflect changes in the base tables.