Db2 views

A view is another way to look at table data—sometimes the whole landscape, sometimes one tree. This page explains what a view is, how views differ from base tables, how to write a simple CREATE VIEW, and a first look at updatable vs read-only views.

Core objects
Progress0 of 0 lessons

What a view is

IBM describes a view as an alternative representation of data that exists in one or more tables. A view can include all or some columns from one or more base tables. Formally, a view is a named specification of a result table. At any moment, the view consists of the rows that its SELECT would return against current data.

Common reasons to create views
GoalHow views help
SecurityOmit salary or PII columns; grant SELECT on the view only
SimplicityPre-join or pre-filter so callers write shorter SQL
StabilityPresent a stable column list while base tables evolve carefully

You retrieve from a view much like a table: SELECT with WHERE, GROUP BY, and HAVING as allowed. Views can be based on tables, other views, or combinations. That composability is powerful—and a reason to keep view layers understandable.

  • Combine data from different base tables
  • Omit columns or rows to shield sensitive data
  • Simplify authorization by granting on the view
  • Show summary or filtered data tailored to a process

Views vs base tables

Base table vs view
AspectBase tableView
What is storedTable description + row dataView definition (SELECT) in the catalog
Indexes / keysAllowed on the tableNot created on the view itself
SELECTYesYes—like a table name in FROM
INSERT/UPDATE/DELETEYes (with privileges)Only if the view is updatable

When you CREATE VIEW, Db2 stores the definition in the catalog. It does not store a separate full copy of the view’s data—the rows live in the underlying base tables. Restriction: you cannot create an index for a view, and you cannot create keys or constraints on a view; build those on the base tables.

Columns added to base tables after the view is defined do not appear in the view automatically. If callers need new columns, recreate or adjust the view definition.

Simple CREATE VIEW

Use CREATE VIEW, give a name (up to 128 characters), optionally list view column names, and supply AS SELECT ….

sql
1
2
3
4
5
CREATE VIEW EMPINFO (EMPLOYEE, FIRSTNAME, LASTNAME, TEAM, JOBTITLE) AS SELECT EMPNO, FIRSTNME, LASTNAME, WORKDEPT, JOB FROM EMP;

EMPINFO renames columns for callers and omits sensitive columns such as salary. Querying the view is like running that SELECT:

sql
1
2
3
SELECT EMPLOYEE, LASTNAME, TEAM FROM EMPINFO WHERE TEAM = 'A00';

Narrow the view with a WHERE clause in the definition so only certain departments appear:

sql
1
2
3
4
5
6
7
CREATE VIEW EMPINFO_AC (EMPLOYEE, FIRSTNAME, LASTNAME, TEAM, JOBTITLE) AS SELECT EMPNO, FIRSTNME, LASTNAME, WORKDEPT, JOB FROM EMP WHERE WORKDEPT = 'A00' OR WORKDEPT = 'C01';

Mental model

CREATE VIEW = save a SELECT under a name. DROP VIEW = remove that saved definition. Privileges on the view control who can use it. The base table remains the source of truth for stored rows.

Updatable vs read-only views (intro)

Whether you can INSERT, UPDATE, or DELETE through a view depends on its definition. If a view includes a foreign key of its base table, insert/update through the view still obeys that referential constraint. If the base table is a parent, deletes through the view follow the same rules as deletes on the base table—when the view allows deletes at all.

Read-only views cannot be used for insert, update, and delete. Complex SELECTs— joins, unions, aggregations, DISTINCT, and similar constructs—often produce read-only views. Simple views on a single base table with a straightforward column list are the usual candidates for updatability (exact rules are version- and definition-specific—verify in the SQL reference before relying on them in production).

sql
1
2
3
4
5
6
7
8
9
10
11
-- Likely closer to updatable: single table, simple columns CREATE VIEW ACTIVE_CUSTOMER AS SELECT CUST_ID, CUST_NAME, STATUS FROM CUSTOMER WHERE STATUS = 'A'; -- Typically read-only: aggregation CREATE VIEW DEPT_HEADCOUNT AS SELECT WORKDEPT, COUNT(*) AS EMP_COUNT FROM EMP GROUP BY WORKDEPT;

For beginners: use views freely for read simplification and security. Treat updatable views as an advanced design choice—many shops prefer updating base tables explicitly and keeping views read-focused.

Authorization pattern

A classic pattern is: revoke broad SELECT on the base table from an app id, create a view with only allowed columns/rows, and GRANT SELECT on the view. Callers never see the hidden columns even with SELECT *.

Explain It Like I'm Five

The real toy box is the base table. A view is a special window you cut in a piece of cardboard: maybe you only see the red toys, or you put two boxes side by side in the window. Looking through the window does not make a second full toy box. Sometimes you are allowed to move toys through the window; sometimes the window is look-only (read-only).

Exercises

  1. Write CREATE VIEW that shows CUST_ID and CUST_NAME only from CUSTOMER.
  2. Explain why a manager might get SELECT on that view but not on CUSTOMER.
  3. Why doesn’t adding EMAIL to CUSTOMER automatically add EMAIL to an old view?
  4. Classify EMPINFO (simple column subset) vs DEPT_HEADCOUNT (GROUP BY) as more likely updatable or read-only, and say why.
  5. Can you CREATE INDEX on EMPINFO? What should you index instead?

Quiz

Test Your Knowledge

1. A Db2 view is best described as:

  • A second physical copy of every base row by default
  • A named specification of a result table (stored definition, data from base objects)
  • Only a buffer pool
  • Only a STOGROUP

2. How does a view differ from a base table?

  • Views always allow CREATE INDEX on the view itself
  • Base tables store persistent data; views present that data (or a subset/join) without owning a separate full copy
  • Base tables cannot be selected
  • Views replace databases

3. CREATE VIEW essentially stores:

  • Only SMF type 30 records
  • The view name and the SELECT that defines its contents
  • A mandatory clustering index
  • A new table space automatically named VIEW

4. Read-only views:

  • Can always be the target of INSERT, UPDATE, and DELETE
  • Cannot be used for insert, update, and delete operations
  • Delete the base table nightly
  • Are illegal in Db2

5. A common reason to use views is:

  • To avoid learning SQL SELECT forever
  • Security and simplicity—show only some columns/rows or combine tables for callers
  • To disable the optimizer
  • To replace the catalog