Junior — Middle
Can you name the types of views in PostgreSQL and explain their features?
sobes.tech AI
Answer from AI
In PostgreSQL, there are several types of views:
-
Regular View — a virtual table defined by an SQL query. When accessed, the query is executed anew, and data is not stored separately.
-
Materialized View — stores the result of a query physically in the database. It allows faster data access but requires periodic refresh (REFRESH) to update the data.
-
Updatable View — a regular view to which INSERT, UPDATE, DELETE operations can be applied if it meets certain conditions (for example, contains data from a single table without aggregate functions).
Example of creating a regular view:
CREATE VIEW active_users AS
SELECT id, name FROM users WHERE status = 'active';
Materialized view:
CREATE MATERIALIZED VIEW sales_summary AS
SELECT product_id, SUM(amount) AS total_sales FROM sales GROUP BY product_id;
Materialized views are useful for speeding up complex queries but require management of data updates.