What Is A View In Dbms

7 min read

What Is a View in DBMS? Understanding Database Views, Their Types, and Practical Uses

A view in DBMS (Database Management System) is a virtual table that derives its data from one or more underlying base tables or other views. Think about it: unlike physical tables, a view does not store data itself; instead, it presents a customized, filtered, or aggregated representation of the data as defined by the query that created it. Views are a fundamental feature of relational DBMSs such as MySQL, PostgreSQL, Oracle, and SQL Server, enabling developers and analysts to simplify complex queries, enhance security, and improve data presentation.


Definition and Core Concept

In relational theory, a view is formally defined as a derived relation—a relation that can be expressed in terms of one or more other relations. The DBMS stores only the definition (the SELECT statement) of the view, not the actual rows of data. When a user queries a view, the system automatically executes the underlying SELECT statement and returns the resulting rows. This abstraction layer allows users to work with data without needing to understand the full schema or the intricacies of joins and aggregations.

Key points to remember:

  • Virtual nature: Views do not occupy storage space; they are computed on demand.
  • Encapsulation: A view can hide the complexity of base tables, exposing only relevant columns and rows.
  • Security: By granting access to a view rather than the base table, administrators can restrict what data users see.

Types of Views

DBMSs support several categories of views, each serving distinct purposes:

1. Simple (Single‑Table) Views

These views are built from a single base table. They are useful for filtering data or presenting a subset of columns.

CREATE VIEW employee_summary AS
SELECT employee_id, first_name, last_name
FROM employees;

2. Complex (Multiple‑Table) Views

Complex views involve joins, unions, or aggregations across multiple tables. They enable users to retrieve consolidated information without writing lengthy queries.

CREATE VIEW order_details AS
SELECT o.order_id, c.customer_name, o.order_date, SUM(od.quantity * od.unit_price) AS total
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
JOIN order_details od ON o.order_id = od.order_id
GROUP BY o.order_id, c.customer_name, o.order_date;

3. Materialized Views

Unlike regular views, materialized views store the result set physically. They are refreshed periodically (or on demand) to reflect changes in the underlying tables. Materialized views are especially valuable for reporting and performance‑intensive queries on large datasets.

4. Indexed Views

In SQL Server, an indexed view is a view with a unique clustered index. The index is built on the view’s result set, which can dramatically speed up query execution for complex aggregations Less friction, more output..

5. Updatable vs. Non‑Updatable Views

  • Updatable views allow INSERT, UPDATE, or DELETE operations directly on the view, provided certain conditions are met (e.g., the view must be based on a single table or a set of tables with clear keys).
  • Non‑updatable views are read‑only, often due to joins, aggregations, or grouping that prevent direct modification.

Creating a View: Step‑by‑Step

  1. Connect to the DBMS using a client tool or application.

  2. Identify the data needed—determine which columns, tables, and transformations are required And it works..

  3. Write the SELECT statement that defines the view. Include any necessary joins, filters (WHERE), grouping (GROUP BY), ordering (ORDER BY), and limiting clauses.

  4. Execute the CREATE VIEW command:

    CREATE VIEW view_name AS
    SELECT ... FROM ...;
    
  5. Grant permissions to users or roles so they can query the view:

    GRANT SELECT ON view_name TO role_name;
    
  6. Test the view by running a simple SELECT statement against it to verify that the data appears as expected.


Advantages of Using Views

  • Simplification: Complex queries become reusable, reducing the need to rewrite involved SELECT statements.
  • Security: Views act as a security barrier, exposing only the columns and rows that users are authorized to see.
  • Abstraction: Changes to underlying table structures can be mitigated by adjusting the view definition, minimizing impact on applications.
  • Performance: Indexed or materialized views can accelerate read‑heavy workloads by pre‑computing results.
  • Data Presentation: Views can format data for specific reports, such as aggregating sales totals or joining customer and order data.

Disadvantages and Limitations

  • Performance overhead: Regular views add processing overhead because the underlying query runs each time the view is accessed.
  • Maintenance complexity: When base tables change, view definitions may need updates to remain valid.
  • Limited updatability: Not all views support INSERT, UPDATE, or DELETE operations, which can restrict data manipulation capabilities.
  • Refresh latency: Materialized views require periodic refreshes, which can lead to stale data if not managed correctly.
  • Query optimizer challenges: In some cases, the optimizer may not fully apply a view’s potential, leading to suboptimal execution plans.

Views and Query Optimization

The query optimizer treats a view as an inline derived table. When evaluating a query that references a view, the optimizer expands the view’s SELECT statement into the main query’s execution plan. This expansion can be beneficial if the optimizer can push predicates, reorder joins, or apply other optimizations effectively Worth knowing..

Tips for optimal view performance:

  • Keep view definitions lean—avoid unnecessary columns, complex functions, or heavy aggregations.
  • Use indexes on frequently queried columns of base tables to speed up view materialization.
  • For reporting, consider materialized views when the view is queried repeatedly and the data changes infrequently.
  • Monitor execution plans to ensure the view is not causing unnecessary sorts or scans.

Security Implications

Views are a cornerstone of role‑based access control (RBAC). By granting privileges on a view rather than the underlying table, administrators can enforce the principle of least privilege:

  • Column masking: Hide sensitive columns (e.g., Social Security numbers) while still allowing access to other fields.
  • Row filtering: Restrict users to see only rows that match certain criteria (e.g., employees seeing only their own department).
  • Complex security views: Combine multiple tables and apply business rules to limit data exposure.

Maintenance Considerations

  • Schema changes: If a base table’s structure changes (column rename, data type alteration), the view may become invalid. Regularly test views after schema modifications.
  • Refresh strategies: For materialized views, define a refresh schedule (e.g., nightly) or a trigger‑based refresh to keep data current.
  • Documentation: Keep view definitions and their intended purpose documented to ease future maintenance and onboarding of new developers.

Practical Example: Sales Dashboard View

Imagine a retail company that wants a sales dashboard summarizing daily revenue by region. A view can simplify this:

CREATE VIEW daily_sales_summary AS
SELECT
    DATE(sale_date) AS sale_day,
    region,
    SUM(amount) AS total_sales,
    COUNT(*) AS transaction_count
FROM sales
WHERE sale_date >= CURRENT_DATE - INTERVAL '30 days'
GROUP BY DATE(sale_date), region;

Analysts can now query daily_sales_summary without writing complex joins or aggregations each time. The view also ensures that only the last 30 days of data are exposed, aligning with privacy and performance requirements.


Frequently Asked Questions (FAQ)

Q: Can a view be used as a table for reporting tools?
A: Yes, most reporting tools can connect directly to a view as if it were a

table. This abstraction layer simplifies data access and can improve security by limiting the underlying tables the tool interacts with.

Q: What is the performance difference between a view and a materialized view?
A: A standard view is essentially a saved query; its logic is executed each time it is accessed, which can be resource-intensive for complex calculations. A materialized view, on the other hand, stores the query result physically and is refreshed periodically. It offers faster query performance at the cost of storage space and the need to manage refreshes to ensure data freshness That's the part that actually makes a difference. No workaround needed..

Q: Can I update data through a view?
A: In many cases, yes, but with limitations. Simple views based on a single table are generally updatable. On the flip side, views involving joins, aggregates, or certain functions often become read-only, as the database cannot unambiguously determine how to propagate changes back to the underlying tables Not complicated — just consistent. That alone is useful..


Conclusion

In the modern database landscape, views transcend their role as simple query shortcuts, emerging as a fundamental tool for both data engineering and governance. In practice, they provide a powerful abstraction layer that simplifies complex queries, enhances security through controlled access, and optimizes performance when used strategically. Even so, by thoughtfully designing views for lean definitions, appropriate indexing, and considering materialization for reporting workloads, organizations can significantly improve both developer productivity and system efficiency. At the end of the day, a well-implemented view strategy is a hallmark of a mature data architecture, balancing flexibility with control to meet the diverse needs of analysts and applications alike Simple, but easy to overlook..

Just Went Up

Recently Written

Readers Went Here

Cut from the Same Cloth

Thank you for reading about What Is A View In Dbms. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home