Sub topic 1
What a View Is and Why It Exists
A view is a named SQL query that you can use as if it were a table. A view usually does not store its own copy of the result. Instead, the database stores the query definition and runs that query when someone reads from the view.
Prerequisites
This chapter assumes that you can write basic SELECT queries, filter rows with WHERE, join tables with JOIN, and create tables with CREATE TABLE. You should also understand that database privileges control what users can read.
A View Is a Named Query
Suppose an application often needs employee names and their department names. The data is stored in two base tables:
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255),
salary NUMERIC(10, 2) NOT NULL,
status VARCHAR(20) NOT NULL,
department_id INTEGER REFERENCES departments (id)
);A base table is a table that physically stores rows. A view can hide the join needed to combine these tables:
CREATE VIEW employee_directory AS
SELECT e.id,
e.name,
d.name AS department
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id;After creating the view, query it like a table:
SELECT id, name, department
FROM employee_directory
WHERE department = 'Engineering';The view has a name, employee_directory, and output columns. Its definition says where those columns come from. The AS keyword gives the database the query that defines the view.
A view does not normally copy the employee and department rows. When the database receives a query against the view, it combines the view query with the outer query, then executes the resulting plan.
This behavior means that a regular view normally reflects current data in its base tables. If an employee changes departments, a later query of the view uses the new relationship.
Why Use a View?
Views provide a stable, named interface for a complex query. This can help in several ways:
- Simplification: Callers use
SELECT * FROM employee_directoryinstead of repeating a join. - Abstraction: The database can change the underlying query while applications continue using the view's name and columns.
- Consistent logic: A shared filter or join is defined once instead of copied into many reports.
- Controlled exposure: A view can return selected columns and rows, such as names and departments without salary or email data.
- Permission boundaries: A user can receive permission to read a view without receiving direct permission on every underlying table, subject to the database system's security rules.
A view does not automatically improve performance. Because a regular view is expanded into the query that uses it, its performance usually depends on the underlying tables, joins, filters, indexes, and query plan. A view can make SQL easier to maintain while leaving execution cost unchanged.
View Compared with a Base Table
| Property | Base table | Regular view |
|---|---|---|
| Stores rows directly | Yes | Usually no |
| Defined by a query | No | Yes |
| Reflects current base-table data | Directly | When queried |
| Can hide columns or joins | Not by itself | Yes |
Views also create dependencies. If a view refers to a column that is later renamed or removed, the view may fail or prevent the schema change, depending on the database system. Treat a view definition as part of the database schema and test changes that affect it.
Exercise
Does a regular view contain an independent copy of the rows returned by its query?
Show answer
Usually no. It stores a query definition, and the database runs that definition when the view is queried.