Database_Views_Enhanced_Presentation
Database_Views_Enhanced_Presentation
• Example:
• CREATE VIEW emp_names AS
• SELECT name FROM employees;
Complex View
• - Based on multiple tables
• - Can include functions, joins, groupings
• Example:
• CREATE VIEW dept_salary AS
• SELECT d.name, AVG(e.salary) FROM
employees e JOIN departments d ON
e.dept_id = d.id GROUP BY d.name;
Materialized View
• - Stores data physically for better
performance.
• - Needs to be refreshed.
• Example:
• CREATE MATERIALIZED VIEW mat_view AS
SELECT * FROM sales;
Creating Views - Syntax
• CREATE VIEW view_name AS
• SELECT columns FROM table WHERE
condition;
Creating Views - Example
• CREATE VIEW active_users AS
• SELECT username, last_login FROM users
WHERE status = 'active';
Using Views in Queries
• SELECT * FROM high_salary;
• SELECT name FROM emp_names WHERE
name LIKE 'A%';
Updating Data Through Views
• - Views can be updatable if based on a single
table without joins or aggregation.
• Example:
• UPDATE high_salary SET salary = salary + 5000
WHERE name = 'Ali';
Limitations of Views
• - Cannot always update
• - Cannot include certain clauses
• - Performance overhead in complex views
Dropping a View
• Syntax:
• DROP VIEW view_name;
• Example:
• DROP VIEW high_salary;
Advantages of Views
• - Simplifies access
• - Provides security
• - Reduces data complexity
• - Enables reusability of queries
Disadvantages of Views
• - Performance issues
• - Not always updatable
• - Maintenance of complex views can be hard
Best Practices
• - Name views clearly
• - Use views for security and abstraction
• - Keep views simple when possible
Real-Life Application Example
• - In a banking system, views can be used to
show account summary, filter active accounts,
or hide sensitive columns.
Summary
• - Views = Virtual Tables
• - Help in abstraction, security, and simplicity
• - Various types: Simple, Complex, Materialized
• - Create/Use/Drop views with SQL