Database View
- In Turkish
- Veritabanı View'ı
In short
A database view is a saved SQL query that behaves like a virtual table, so you can select from it by name instead of repeating the underlying query each time.
What is a database view?
A database view is a named query stored in the database. You create it once with CREATE VIEW, and after that you can query it just like a table. A regular view does not store any data of its own; each time you query it, the database runs the underlying SELECT against the real tables.
When you query a view, the database merges the view's definition into your query and optimizes the combined statement, so the result always reflects the current data. Views are used to hide complicated joins behind a simple name, to expose only some columns or rows to certain users by granting access to the view but not to the base tables, and to keep a stable interface while the underlying tables change. Simple views over a single table can often be updated directly, while complex ones are read-only.
A view is like a saved playlist: it doesn't copy the songs, it only remembers which ones to show and in what order. A materialized view is different: it runs the query once and stores the result as a real table, so reads are fast but the data can be stale until the view is refreshed. Materialized views are common for dashboards and reports that aggregate large tables.
The common confusion is between a view and a table, or between a view and a stored procedure. A table holds data; a regular view holds only a query. A stored procedure is a program you call, which can take parameters and change data, while a view is a result set you read from inside a SELECT. On its own, a regular view does not make queries faster, because the database still does the same work underneath.
Key takeaways
- A view is a named, saved query that you can select from like a table.
- A regular view stores no data and always shows current results.
- Views simplify complex joins and can limit which columns users see.
- A materialized view stores its result and must be refreshed to stay current.
Example
-- A view hides a join behind a simple name
CREATE VIEW active_customers AS
SELECT c.id, c.name, c.country, MAX(o.created_at) AS last_order
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name, c.country;
SELECT * FROM active_customers WHERE country = 'DE';
-- A materialized view stores the result; refresh it to update
CREATE MATERIALIZED VIEW sales_by_month AS
SELECT date_trunc('month', created_at) AS month, SUM(total) AS revenue
FROM orders GROUP BY 1;
REFRESH MATERIALIZED VIEW sales_by_month;Readers ask
Does a database view store data?
A regular view does not; it stores only the query and runs it each time you read from the view. A materialized view does store its result, which is why it needs to be refreshed.
What is the difference between a view and a materialized view?
A view is recomputed on every query, so it is always current but can be slow for heavy queries. A materialized view is precomputed and fast to read, but it shows the data as of its last refresh.
Can you insert or update data through a view?
Often yes for simple views that select from a single table without aggregates or DISTINCT. Views with joins or grouping are usually read-only unless the database offers special rules or INSTEAD OF triggers.
See also
- SQLDatabases, p. 40SQL is the standard language for working with relational databases, used to create tables and to insert, query, update, and delete the data stored in them.
- Database SchemaDatabases, p. 11A database schema is the blueprint of a database that defines its tables, columns, data types, relationships, and the rules that stored data must follow.
- Stored ProcedureDatabases, p. 44A stored procedure is a named set of SQL statements saved inside the database, which applications can run with a single call instead of sending each query.
- SQL JOINDatabases, p. 41A SQL JOIN is a query operation that combines rows from two or more tables into one result, matching them on related columns such as a foreign key.
- AuthorizationSecurity, p. 3Authorization is the process of deciding what an authenticated user or service is allowed to do, such as which data it can read, change, or delete.
- Database TriggerDatabases, p. 12A database trigger is code stored in the database that runs automatically when a chosen event, such as an insert, update, or delete, happens on a table.
Spotted a mistake or something missing on this page?Suggest an edit