Views
SQL VIEWS
1. AIM
To study the concept of Views in SQL, understand their advantages, and perform operations such as creating, modifying, updating, and dropping views in MySQL.
2. THEORY
✔ What is a View?
A view is a virtual table created from one or more tables in a database.
-
It does not store data physically.
-
It stores only the query definition.
-
Whenever a view is accessed, the query is executed dynamically.
-
Views help in security, simplification, and customization of data.
✔ Key Properties
| Concept | Description |
|---|---|
| Virtual Table | Looks like a table, but does not store data |
| Security | Restricts users from accessing sensitive columns |
| Simplification | Presents complex joins in a simplified way |
| Read-only or Updatable | Some views allow UPDATE/INSERT, others don’t |
| No Storage | Except for materialized views (not in MySQL) |
3. VIEW OPERATIONS IN MYSQL
✔ Create a View
✔ View the View Structure
✔ Display View Content
✔ Modify (Replace) a View
✔ Drop a View
4. EXAMPLE DATABASE FOR LAB
Create a sample table:
5. VIEW EXAMPLES WITH OUTPUTS
Experiment 1: Simple View
Create a view showing only selected columns
View data:
Output
| emp_id | emp_name | dept |
|---|---|---|
| 1 | Alice | HR |
| 2 | Bob | Finance |
| 3 | Charlie | IT |
| 4 | David | IT |
| 5 | Eve | HR |
Experiment 2: Conditional View
Output
| emp_name | salary |
|---|---|
| Charlie | 70000 |
Experiment 3: View on Multiple Tables
Suppose you have:
Join view:
Experiment 4: Updating Through a View
View:
Update using the view:
✔ Allowed because:
-
Single base table
-
No GROUP BY
-
No DISTINCT
Experiment 5: Create or Replace View
Experiment 6: Dropping a View
6. TYPES OF VIEWS
| Type | Description |
|---|---|
| Simple View | Based on one table, no functions |
| Complex View | Based on multiple tables, may use aggregate functions |
| Read-Only View | Not updatable due to joins or aggregations |
| Updatable View | Supports UPDATE/DELETE if MySQL rules allow |
7. BENEFITS OF VIEWS
-
Hide sensitive data
-
Simplify complex queries
-
Provide logical data independence
-
Maintain consistent representation of data
-
Useful for security and abstraction
8. LAB PRACTICE QUESTIONS
-
Create a view to display employees from the HR department.
-
Create a view to show employees earning between 50,000 and 60,000.
-
Create a join view displaying employee name, department, and location.
-
Update employee salary using a view.
-
Create a view that shows the highest salary in each department (GROUP BY).
-
Try updating a complex view and observe the error.
-
Drop a view and confirm it is removed.
9. RESULT
The concepts of Views were successfully implemented. Students created, modified, updated, and deleted views and understood their use in simplifying queries and enhancing database security.
Comments
Post a Comment