Sub topic 3
Naming the Columns of a View
This chapter assumes you can write SELECT queries, join tables, create tables, and use column aliases. A view column is an output column produced by the query stored in a view. Clear names make that output easier to query and safer to use in applications.
Why view column names matter
A view can select columns directly from tables:
CREATE VIEW employee_directory AS
SELECT e.id,
e.name,
d.name
FROM employees AS e
JOIN departments AS d ON d.id = e.department_id;This view has two output columns named name. Some database systems reject this definition. Others create an ambiguous result that is difficult to query. Give each output column a distinct name instead:
CREATE VIEW employee_directory AS
SELECT e.id,
e.name AS employee_name,
d.name AS department_name
FROM employees AS e
JOIN departments AS d ON d.id = e.department_id;You can now refer to the columns by their intended meanings:
SELECT employee_name, department_name
FROM employee_directory
WHERE department_name = 'Engineering';An alias is a temporary name assigned with AS. In a view definition, the alias becomes the name exposed by the view.
Naming expressions
Expressions do not always have useful default names. For example, a calculation might be exposed as an implementation-specific name or as the complete expression text.
CREATE VIEW employee_summary AS
SELECT id AS employee_id,
name AS employee_name,
salary / 12 AS monthly_salary
FROM employees;The view now exposes a stable, readable interface:
| View column | Meaning |
|---|---|
| `employee_id` | Employee's identifier |
| `employee_name` | Employee's name |
| `monthly_salary` | Salary divided by 12 |
Use names that describe the value, not the calculation used to produce it. A later change from salary / 12 to another monthly-pay calculation should not require users of the view to change their queries.
Using a column list
You can name all view columns immediately after the view name. The names are matched by position with the expressions in the SELECT list.
CREATE VIEW employee_summary
(employee_id, employee_name, monthly_salary) AS
SELECT id,
name,
salary / 12
FROM employees;This produces the same column names as the previous example. A column list is useful when:
- You want the public names in one obvious place.
- The
SELECTlist contains several expressions. - You want to avoid repeating aliases inside the query.
- You want to make the view's interface easy to review.
Do not mix a view column list with a different number of selected expressions. The database should reject that definition.
Choosing a naming style
Choose one naming convention and use it consistently. For example, a team might use snake_case, singular identifiers, and names that identify units.
CREATE VIEW department_pay AS
SELECT department_id,
SUM(salary) AS total_salary,
AVG(salary) AS average_salary
FROM employees
GROUP BY department_id;Names such as total_salary are clearer than sum. If a value has a unit, include it when ambiguity is likely, such as monthly_salary or duration_seconds.
Avoid reserved words and vague names such as value, data, or result. Also avoid changing a view's column names casually. Applications and reports may depend on them.
Exercise
What is the difference between d.name AS department_name and a view column list?
Show answer
The alias names one selected expression where it appears. A view column list names all selected expressions by their position after the view name.