What is a database view, and what are its uses?
Instruction: Define what a database view is and discuss its common uses and advantages.
Updated
Official answer
An ordinary view stores a query definition and presents its result as a relation. It can provide a stable interface, centralize a join/filter, or expose selected columns.
For PostgreSQL products(product_id, name, internal_cost), a simple view is:
CREATE VIEW product_names AS
SELECT product_id, name FROM products;
This does not cache the names: later queries read according to transaction visibility. A materialized view stores results and needs an explicit refresh policy; freshness and refresh cost become part of its contract. An ordinary view does not automatically make an expensive query faster.
If the view is an access interface, configure and test privileges carefully. PostgreSQL normally checks underlying relation access using the view owner's permissions; a security_invoker view uses the caller's permissions instead. Unrestricted base-table access can bypass the intended restriction. Row-security policies and functions used by the view require additional attention.
Avoid SELECT * in a long-lived contract when unintended columns would be harmful. Test representative consumers and permissions when changing the view. A view is useful abstraction, but creating one alone does not establish security, freshness or performance guarantees.