SQL Views
Common Challenges
Building Complex Queries
Today's Topic: SQL Views
Learning Objectives
What is a SQL View? Definition
What is a SQL View? Analogy & Concept
Key Concept: Views Store Definitions, Not Data
Views vs. Base Tables
Why Use Views? Benefits & Use Cases
Benefit 1: Simplicity
CREATE VIEW v_employee_department AS
SELECT e.name AS employee_name, d.name AS department_name
FROM employees e JOIN departments d ON e.dept_id = d.id;
SELECT * FROM v_employee_department;
Benefit 2: Security / Access Control
CREATE VIEW v_public_employee_info AS
SELECT id, name, dept_id FROM employees;
CREATE VIEW v_sales_staff AS
SELECT id, name FROM employees WHERE dept_id = (SELECT id FROM departments WHERE name = 'Sales');
Benefit 3: Logical Data Independence
Benefit 3: Logical Data Independence
Working with Standard Views: Creating Views
CREATE VIEW view_name [(column_list)]
AS
SELECT_statement;
Creating Views: PostgreSQL Example
CREATE VIEW v_employee_dept_names AS
SELECT
e.name AS employee_name, -- Alias for clarity
d.name AS department_name -- Alias for clarity
FROM
employees e
JOIN
departments d ON e.dept_id = d.id
WHERE
d.name = 'Sales';
Working with Standard Views: Querying Views
SELECT columns
FROM view_name
[WHERE condition]
[ORDER BY ...];
Querying Views: PostgreSQL Example
SELECT employee_name
FROM v_employee_dept_names
ORDER BY employee_name;
Working with Standard Views: �Modifying Views
CREATE OR REPLACE VIEW view_name [(column_list)]
AS
new_SELECT_statement;
Modifying Views: PostgreSQL Example
CREATE OR REPLACE VIEW v_employee_dept_names AS
SELECT
e.name AS employee_name,
d.name AS department_name
FROM
employees e
JOIN
departments d ON e.dept_id = d.id;
-- Removed the WHERE clause
Working with Standard Views: Dropping Views
DROP VIEW [IF EXISTS] view_name [, ...] [CASCADE | RESTRICT];
Dropping Views: PostgreSQL Example
DROP VIEW IF EXISTS v_employee_dept_names RESTRICT;
DROP VIEW v_employee_dept_names CASCADE;
Updatability of Views: The Concept
Automatically Updatable Views �("Simple Views")
Non-Updatable Views ("Complex Views")
Updatability Example: Simple View
CREATE OR REPLACE VIEW v_sales_employees AS
SELECT id, name, salary, dept_id
FROM employees
WHERE dept_id = 1;
This view references only the employees table and has no aggregation, joins, etc. It should be updatable.
Updating a Simple View
UPDATE v_sales_employees
SET salary = salary * 1.10
WHERE id = 123;
Updatability Example: �Non-Updatable Complex View
CREATE OR REPLACE VIEW v_employee_dept_names AS
SELECT e.name AS employee_name, d.name AS department_name
FROM employees e JOIN departments d ON e.dept_id = d.id;
INSERT INTO v_employee_dept_names (employee_name, department_name)
VALUES ('New Hire', 'Sales');
Making Complex Views Updatable (Advanced)
Beyond Standard Views: Materialized Views
Why Use Materialized Views? Performance
Creating Materialized Views
CREATE MATERIALIZED VIEW view_name
AS
SELECT_statement
[WITH [NO] DATA]; -- Default is WITH DATA (populate at creation)
Creating Materialized Views: Example
CREATE MATERIALIZED VIEW mv_department_salary_summary AS
SELECT
d.name AS department_name,
AVG(e.salary) AS avg_salary,
COUNT(e.id) AS num_employees
FROM
employees e
JOIN
departments d ON e.dept_id = d.id
GROUP BY
d.name;
Refreshing Materialized Views: The Catch!
REFRESH MATERIALIZED VIEW view_name;
Refreshing Materialized Views: Stale Data Trade-off
Querying and Dropping Materialized Views
SELECT * FROM mv_department_salary_summary WHERE avg_salary > 60000;
DROP MATERIALIZED VIEW [IF EXISTS] view_name;
Lecture Summary: Key Concepts
Summary: Standard Views
CREATE VIEW view_name AS ...
CREATE OR REPLACE VIEW view_name AS ... (Modify definition)
SELECT ... FROM view_name ... (Querying)
DROP VIEW [IF EXISTS] view_name [CASCADE | RESTRICT]
Summary: View Updatability
Summary: Materialized Views
CREATE MATERIALIZED VIEW mv_name AS ...
REFRESH MATERIALIZED VIEW mv_name; (Crucial for updating data)
SELECT ... FROM mv_name ... (Querying)
DROP MATERIALIZED VIEW [IF EXISTS] mv_name;