Home › Theme A › A3 › A3.3 › A3.3.5

A3.3.5 Database views

HL onlyCommand term: Describe

1. From base tables to views

A view is a saved SELECT query that users can treat like a table. Press Next to create a virtual view, change the data, then create a materialised view and watch it go out of date.

2. Live simulation: virtual vs materialised

Change the base table, then query both views. The virtual view re-runs its query every time. The materialised view reads its stored copy, which goes stale (orange) until you refresh it. Watch the work counters.

Pick an action.

3. Security lab: who sees what?

Each staff role is granted access to certain views only. Choose a role, then try the statements. Notice which ones are denied and which views cannot be updated.

4. Flip-card deck

Click to flip. Say the answer before you flip.

5. Which benefit of views?

Each scenario uses a view. Which benefit does it show best? Streaks of 3+ score double.

Round 0/10Score 0Streak 0Best 0
Press start!

6. 45-second speed round: virtual, materialised or both?

Which type of view does the statement describe?

Time 45sScore 0Streak 0Best 0
Press start!

Learning objectives

By the end of this page you should be able to:

  1. Describe what a database view is.
  2. Distinguish between virtual views and materialised (snapshot) views.
  3. Describe how views hide data complexity, simplify queries and improve security.
  4. Describe how views affect data consistency, independence and performance.
  5. Distinguish between read-only and updatable views.

What is a view?

A view is a named, stored SELECT query. Users query it as if it were a table, but it presents data taken from one or more base tables (the real tables).

CREATE VIEW GradeReport AS SELECT STUDENT.StudentName, ENROLMENT.CourseCode, ENROLMENT.Grade FROM STUDENT JOIN ENROLMENT ON STUDENT.StudentID = ENROLMENT.StudentID; SELECT * FROM GradeReport WHERE Grade >= 6;

Two kinds of view

Virtual viewMaterialised (snapshot) view
What is storedOnly the query definitionThe query and its result rows, on disk like a table
When is it calculated?Every time it is queriedWhen it is created or refreshed
Up to date?Always shows the current dataCan be stale until refreshed (on a schedule, on demand or on commit)
SpeedAs slow as the underlying query (joins, aggregates)Fast to read: the work is already done
StorageAlmost noneExtra storage for the copy
Good forSecurity, simplification, live dataExpensive reports and dashboards on large data
Syntax (varies)CREATE VIEW ...CREATE MATERIALIZED VIEW ..., REFRESH MATERIALIZED VIEW ...

Why use views?

  • Security: grant users access to a view, not the base table, so they see only certain columns (no phone numbers or medical notes) or certain rows (only their own class). GRANT SELECT ON GradeReport TO teacher;
  • Hiding data complexity: users do not need to know how the data is split across tables, keys and joins.
  • Query simplification: a long join or calculation is written once; users write SELECT * FROM GradeReport.
  • Data consistency: everyone uses the same definition (e.g. how “average grade” is calculated), so reports agree.
  • Independence (logical data independence): if base tables are restructured, the view can be redefined so applications using it do not change.
  • Performance: materialised views store pre-calculated results, so heavy reports load quickly. (A virtual view does not by itself make a query faster.)

Read-only or updatable?

A view is usually updatable when it is based on one table, with no aggregates, GROUP BY or DISTINCT. An INSERT, UPDATE or DELETE on it is passed to the base table. Views that join tables or aggregate data are usually read-only. Materialised views are normally read-only: change the base tables and refresh. Making a view read-only is itself useful: staff can look but not change.

Use the 中文 button at the top to show or hide the Chinese terms.

Term中文Meaning

Choose an answer. You get instant feedback.

Score: 0 /

Write your answer first, then reveal the markscheme. These are exam-style practice questions written for this site.

1. Define the term view in a relational database. [2]

A virtual table / stored (named) query [1] that presents data from one or more base tables, which users can query like a table [1].

2. Outline one difference between a virtual view and a materialised view. [2]

A virtual view stores only the query and calculates its result each time it is used / is always up to date [1]; a materialised view stores the result rows physically and must be refreshed, so it can be out of date [1]. Accept storage or speed differences if both sides are given.

3. A school's STUDENT table contains phone numbers and medical notes. Describe how a view could be used to improve security. [3]

Create a view that selects only the columns teachers need (e.g. ID, name, tutor group) [1]; grant teachers permission on the view but not on the base STUDENT table [1]; so the sensitive columns are hidden from them / they cannot access or change the full data [1]. Accept restricting rows (e.g. only their own class) as an alternative point.

4. Explain why a materialised view may improve the performance of a dashboard that summarises millions of sales records. [3]

The summary (joins / aggregates such as SUM, COUNT) is calculated once and the results are stored [1]; each time the dashboard loads it reads the small stored result instead of processing millions of rows [1]; so responses are much faster / load on the database is reduced [1]. Accept: the trade-off is that data may be stale until refreshed.

5. Explain why a view that joins STUDENT and ENROLMENT is often read-only, while a view of selected columns from STUDENT can be updatable. [4]

A view on a single table maps each of its rows to exactly one row of the base table [1], so an UPDATE can be passed directly to that row [1]. A view that joins tables combines rows from two tables / may repeat data [1], so the database cannot always tell which base table or row a change should go to, and the view is treated as read-only [1]. Accept aggregates / GROUP BY making a view read-only.

6. Describe three benefits of using views in a database. [6]

Award [2] for each benefit described, up to [6]: Security [1]: users can be given access to a view showing only certain rows or columns [1]. Query simplification / hiding complexity [1]: complex joins and calculations are written once and users query the view simply [1]. Data consistency [1]: everyone uses the same definition, so results agree [1]. Data independence [1]: base tables can change while the view keeps the same structure for applications [1]. Performance (materialised) [1]: pre-computed results are read quickly [1]. Read-only access [1]: users can view but not change data through a read-only view [1].