Views (Virtual Tables) in SQL
Views (Virtual Tables) in SQL
A view is one of the most important SQL abstractions. It supports simplicity, security, and logical data independence.
1. What Is a View?
A view is:
-
A virtual table
-
Derived from one or more base tables (or other views)
-
Defined using a SELECT query
-
Not stored physically (in most cases)
📌 Base tables → physically stored
📌 Views → logically stored as definitions
Why “Virtual Table”?
-
A view behaves like a table for querying
-
But its tuples are computed dynamically
-
Data is fetched from base tables when the view is queried
👉 Hence, views are always up-to-date
2. Motivation for Views
Views are used to:
-
Simplify complex queries
-
Provide security and restricted access
-
Present customized data
-
Hide complexity of joins
-
Support logical data independence
3. Example Scenario (COMPANY Database)
Suppose we frequently need:
-
Employee names
-
Project names
-
Hours worked
Instead of repeatedly joining:
-
EMPLOYEE
-
WORKS_ON
-
PROJECT
👉 We define a view once, then query it like a table.
4. Creating Views in SQL (CREATE VIEW)
General Syntax
5. View Example 1: WORKS_ON1
Explanation
-
View name:
WORKS_ON1 -
Derived from three tables
-
No new attribute names specified
-
Attribute names inherited automatically
📌 WORKS_ON1 schema:
6. View Example 2: DEPT_INFO (with aggregation)
Explanation
-
Explicit attribute renaming using CREATE VIEW
-
Uses GROUP BY and aggregate functions
-
Produces one row per department
📌 This is a summary view
7. Querying a View
Once created, a view is queried just like a table.
Example Query on View
✔ No joins required
✔ Much simpler than querying base tables
8. Dropping a View
-
Removes the view definition
-
Does not affect base tables
9. View Implementation Strategies
9.1 Query Modification (View Expansion)
-
DBMS rewrites view query into base-table query
-
View definition is substituted
Example:
Automatically becomes:
📌 Pros: No storage overhead
📌 Cons: Slow for complex views used frequently
9.2 View Materialization
-
View is physically stored
-
Stored as a temporary or permanent table
-
DBMS keeps it synchronized with base tables
Update Strategies:
-
Immediate: update view instantly
-
Lazy: update when queried
-
Periodic: update at intervals (may be stale)
📌 Used in data warehouses
10. Updating Views (Major Issue)
10.1 Why View Updates Are Hard
-
Views may involve:
-
Joins
-
Aggregation
-
Derived attributes
-
-
One view update may map to multiple base-table updates
👉 DBMS cannot always determine the user’s intention
11. Example: Ambiguous View Update
Possible Interpretations:
(a) Update WORKS_ON (correct intention)
-
Change project assignment of John Smith
(b) Update PROJECT (wrong side effect)
-
Rename ProductX to ProductY for all employees
📌 Because multiple interpretations exist, DBMS rejects the update
12. Non-Updatable Views
Example: Aggregate View Update
🚫 Invalid because:
-
Total_salis derived -
Cannot be directly updated
13. When Is a View Updatable?
General Rules
✔ View is updatable if:
-
Defined on single base table
-
Includes primary key
-
No aggregation, GROUP BY, DISTINCT, joins
🚫 View is NOT updatable if:
-
Uses joins
-
Uses aggregate functions
-
Uses GROUP BY / HAVING
14. WITH CHECK OPTION
✔ Ensures updates through the view do not violate view condition
Example:
-
Cannot insert employee with
Dno ≠ 5through this view
15. Inline Views (Subquery in FROM Clause)
A view can be defined inside a query:
📌 Exists only for that query execution
16. Views as Authorization Mechanisms
Views help restrict access.
Restrict Rows
✔ User sees only department 5 employees
Restrict Columns
✔ Sensitive attributes (Salary, SSN) hidden
17. Advantages of Views
✔ Simplify complex queries
✔ Improve security
✔ Logical data independence
✔ Data abstraction
✔ Reusability
18. Summary Table
| Aspect | Views |
|---|---|
| Storage | Virtual (logical) |
| Data | Always up-to-date |
| Querying | Fully supported |
| Updates | Limited |
| Security | Excellent |
| Complexity | Hidden |
Final Takeaway
A view is a virtual table defined by a query that simplifies data access, improves security, and provides logical abstraction—but with restrictions on updates.
Comments
Post a Comment