In a Database Management System (DBMS), a view is a virtual table that is based on the result of a SELECT query. Unlike a physical table, a view does not store the actual data itself; instead, it is a saved query that can be referenced as if it were a table. Views are useful for simplifying complex queries, encapsulating business logic, and providing a layer of security by restricting access to specific columns or rows of a table. Here are key points about views in a DBMS:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
CREATE VIEW employee_view AS
SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id = 1;
Using a View:
Once a view is created, you can use it in SELECT statements as if it were a table.
SELECT * FROM employee_view;
In some cases, you can update the underlying tables through a view, but there are restrictions, and it depends on the complexity of the view. Simple views based on a single table can often be updated, but complex views involving multiple tables or aggregation functions may have limitations.
DROP VIEW view_name;
Difference between Normal View and Materialized View
The main difference between views and materialized views is that views are dynamic and materialized views are static. This means that views always reflect the latest data from the underlying tables or views, while materialized views only show the data from the last refresh. Therefore, views are more suitable for queries that need real-time data, while materialized views are more suitable for queries that need precomputed data.
Another difference is that views are more lightweight and flexible, while materialized views are more resource-intensive and rigid. This means that views do not take up any storage space or require any maintenance, while materialized views do. However, views also depend on the availability and performance of the underlying tables or views, while materialized views do not. Therefore, views are more convenient for creating temporary or ad hoc queries, while materialized views are more reliable for creating permanent or recurring queries.