Junior — Middle
Explain the role and purpose of the View component in PostgreSQL.
sobes.tech AI
Answer from AI
In PostgreSQL, the View component is a virtual table formed based on the result of an SQL query. A View does not physically store data but only saves the query executed when accessed.
Role of View:
- Simplifies complex queries by providing a convenient interface for reuse.
- Enhances security — access to certain columns or rows can be restricted, providing access only to the View.
- Abstracts the database structure, allowing internal tables to be changed without affecting client code.
Example of creating a View:
CREATE VIEW active_users AS
SELECT id, name, email FROM users WHERE status = 'active';
Now, active_users can be accessed as a regular table.