First exams 2027 · HL only

A3.3.5 · Database programming

Database views

A hotel's housekeeper, its receptionist and its accountant all need the same guest table, and not one of them should see the same columns. A view is how one table becomes three.

HL only Command term: describe Builds on A3.3.4

Explainer

The GUEST table holds a passport number and the last four digits of a payment card. Housekeeping needs to know which rooms are occupied and for how long. Handing them the whole table to get two useful columns is both awkward and a genuine security problem. The database's answer is to define, once, a named query that shows only what that role should see.

Df

A view looks and behaves like an ordinary table from the outside: you write CREATE VIEW once, then query it exactly like any other table afterward. What it actually is underneath is a saved SELECT statement, dressed up to present data from one or more real tables in whatever shape a particular reader needs.

Multiple views can sit over the same base tables, each showing a different subset to a different kind of user.

St

A view stores the query, not the data. This is the single idea the whole statement rests on. Nothing is duplicated, so there is no second copy to keep in step with the original. When someone queries the view, the stored SELECT runs against the live base tables at that moment. Change a row in GUEST and every view over it reflects the change immediately, because there was never anything else to update.

Why a database uses them

Hiding complexity

A view can wrap a complicated multi-table query in one name, so a user can query it without needing to understand the SQL underneath.

Data consistency

Every reader querying the same view gets the same shape of answer back, no matter how much the tables underneath have changed since yesterday.

Data independence

The schema underneath can change without breaking what the user sees, as long as the view definition is updated to match.

Performance

Write the expensive join once, inside the view, and every future user just calls the view by name instead of retyping that same query from scratch.

Query simplification

A large problem can be broken into smaller named pieces, with filters and calculations already applied.

Read-only or updatable

Some views can be written through to the base tables, and some cannot. The conditions are set out further down this page.

Security

Leave a column out of the view's own SELECT, and no permission trick is needed, that user simply has no way to reach it through this view at all.

Explainer

Virtual views and materialized views

The syllabus asks you to describe two kinds. The difference is exactly the one the definition above set up: whether the view stores only the query, or stores the answer as well.

Virtual · type 1

Simple view

Reads from exactly one table and nothing fancier than picking which columns to show, no join, no aggregate function anywhere in it.

Example: Step 2 of the stepper below.

Virtual · type 2

Complex view

Pulls from more than one table, or does something heavier than a plain field pick, a JOIN, a GROUP BY, an aggregate, anything that makes the underlying SELECT genuinely non-trivial.

Example: Step 3 of the stepper below.

Not virtual

Materialized view

Stores a pre-computed result set from its SELECT query, refreshed periodically rather than re-run on every query.

Example: Steps 4 and 5 of the stepper below.

Vs

Simple and complex views are both virtual: they store nothing but the query, and they re-run it every single time they are read.

A materialized (snapshot) view breaks that rule deliberately. It runs the query once, stores the result, and serves that stored result to readers until it is refreshed. It is the only kind of view that holds data of its own.

Choosing between them

This is A3's guiding question in miniature, what are the principles, structures and operations that form the basis of database systems, applied to a single decision: is it more important that the answer is current, or that the answer is fast?

Virtual view

Real example: the hotel's front desk needs to know which rooms are occupied right now. A booking made thirty seconds ago must appear.

Advantage

  • Always current (the query runs against the live base tables at read time, so it cannot be out of date)
  • Uses no extra storage (only the query definition is saved, not a second copy of the rows)
  • Nothing to maintain (there is no refresh schedule to set, forget, or get wrong)

Disadvantage

  • Slower on heavy queries (a join or aggregate over a large table is recalculated in full every single time someone reads the view)
  • Repeated cost (a hundred users reading the same report means the same expensive calculation runs a hundred times)

Materialized (snapshot) view

Real example: the accountant's monthly revenue report, aggregated over every stay in the hotel's history. Nobody minds if it was calculated at 3am.

Advantage

  • Much faster to read (the answer already exists, so reading it avoids re-running the query entirely)
  • Cost paid once (one refresh serves every reader until the next refresh, instead of one calculation per reader)

Disadvantage

  • Can be out of date (it shows the data as it was at the last refresh, so any change since then is invisible)
  • Uses real storage (unlike every other view, it holds an actual copy of the result set on disk)
  • Needs refreshing (someone has to decide how often, and that schedule is a trade-off between freshness and cost all over again)

Worked example

Three views on one hotel

Two base tables, three roles, three different views. Click through and watch the same data get presented three different ways.

Step 1 of 6 Base tables

Explainer

Read-only or updatable

Since a virtual view stores no data of its own, writing to one is really a request to write to the tables underneath it. The database allows that only when it can work out, without ambiguity, which base row each change belongs to.

Up

An updatable view passes changes through to the underlying tables it was created from. If the conditions below are not met, the view is read-only and can be queried but not written to.

The three conditions for an updatable view

1

Trace the view back to one single table (or one already-updatable view) and nothing else. One source only, so a written change has exactly one place to land, never a choice between two.

2

Every base-table field left out of the view must allow NULL values. Inserting through the view can only fill the columns the view exposes, so any column it cannot reach has to be able to sit empty.

3

The view's own SELECT has to stay simple, no sub-query buried inside it. In practice that also rules out anything which collapses or combines rows before they reach the output: grouping with HAVING, de-duplicating with DISTINCT, summarising with an aggregate, or pulling rows together from more than one table. Every one of those breaks the same thing: a clean one-to-one link between an output row and a single base row.

!

Look at what condition 3 excludes and the pattern behind all three conditions becomes obvious. Every excluded feature destroys the one-to-one link between a row in the view and a row in a base table. The hotel's occupancy view (Step 3) joins two tables, so it is read-only. Its revenue view (Step 4) uses SUM, so it is read-only. Only the plain single-table housekeeping view (Step 2) could ever be written through.

Check your understanding

A row is updated in the base table. Which kind of view might still show the old value afterwards?

Common mistake

Markers reported this pattern for A3.3.5

RecurringViews are widely described as "a copy of part of a table." That description is wrong for every virtual view and only accidentally right for a materialized one, and once a candidate holds it, the rest of the statement collapses: they cannot say why a view is always up to date, why most views are read-only, or what actually separates the two types the syllabus names.

✕

What a weak answer looks like: "A view is a copy of selected columns from a table that users can look at instead of the real data." If it were a copy, it would need syncing, and the consistency and independence benefits would not exist.

✓

What it should say: "A view is a stored SELECT query that is presented as a virtual table. It holds no data of its own, so it reads the live base tables each time it is queried. A materialized view is the exception: it stores its pre-computed result set and is refreshed periodically." The stored-query idea comes first, then the exception.

Revision questions

Past-paper style. Write an answer, then reveal the marking points and mark yourself.

Question 1 · 2 marks

[2]Describe what is meant by a database view.

Question 2 · 4 marks

[4]Distinguish between a virtual view and a materialized view.

Question 3 · 5 marks

[5]A hotel gives housekeeping staff access to a view rather than to the GUEST table itself. Describe two benefits of this decision, and explain why the view is likely to be read-only.

Flashcards

Active recall for this statement's key terms.