First exams 2027 · HL only2027 年首次开考 · 仅 HL

A3.3.5 · Database programming

A3.3.5 · 数据库编程

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
仅 HL 指令词:describe 建立在 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.

GUEST 表里有护照号码和银行卡的最后四位数字。客房部只需要知道哪些房间有人住、住了多久。为了拿两个有用的列,把整张表交给他们,既别扭,又是真正的安全问题。数据库的答案是定义一次、给它起个名字的查询,只显示该角色应该看到的内容。

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.

视图(view)从外部看,外观和行为都像一张普通的表:你写一次 CREATE VIEW,之后就可以像查询任何其他表一样查询它。它在底层实际是一条保存下来的 SELECT 语句,被包装成以某位读者所需的任何形状,呈现来自一张或多张真实表的数据。

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.

视图存储的是查询,不是数据。这是整条陈述所依赖的唯一的核心思想。没有任何东西被复制,所以没有第二份副本需要与原件保持同步。有人查询视图时,存储的 SELECT 在那一刻针对实时的基础表运行。改了 GUEST 中的一行,建立在它之上的每个视图都立刻反映这个变化,因为本来就没有别的东西需要更新。

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.

视图可以把一个复杂的多表查询包装成一个名字,这样用户查询它,而不需要理解底层的 SQL。

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.

只要在视图自己的 SELECT 中省略某一列,就不需要任何权限技巧,该用户通过这个视图根本无法访问它。

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
虚拟 · 类型 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.

例子:下面步进器的第 2 步。

Virtual · type 2
虚拟 · 类型 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.

从不止一张表取数据,或者做了比单纯选字段更复杂的事,比如 JOIN、GROUP BY、聚合,任何让底层 SELECT 真正不简单的东西。

Example: Step 3 of the stepper below.

例子:下面步进器的第 3 步。

Not virtual
非虚拟

Materialized view

物化视图

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

存储其 SELECT 查询预先计算好的结果集,定期刷新,而不是每次查询都重新运行。

Example: Steps 4 and 5 of the stepper below.

例子:下面步进器的第 4 步和第 5 步。

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?

这是 A3 的引导性问题的缩影,构成数据库系统基础的原理、结构和操作是什么,应用到一个具体的决定上:答案是最新的更重要,还是答案很快更重要?

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.

基础表中被视图省略的每个字段,都必须允许 NULL 值。通过视图插入时,只能填写视图暴露出来的列,所以它够不到的任何列,都必须能保持为空。

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.

视图自己的 SELECT 必须保持简单,里面不能埋着子查询。实际上这也排除了任何在行到达输出之前就把行折叠或合并的东西:用 HAVING 分组、用 DISTINCT 去重、用聚合汇总,或把来自多张表的行拉到一起。这些都破坏了同一样东西:输出行与单个基础行之间干净的一对一联系。

!

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.

看看条件 3 排除了什么,三个条件背后的规律就变得显而易见。每一个被排除的特性,都破坏了视图中的一行与基础表中的一行之间的一对一联系。酒店的入住视图(第 3 步)连接了两张表,所以是只读的。它的收入视图(第 4 步)使用了 SUM,所以是只读的。只有普通的单表客房部视图(第 2 步)才有可能被写入。

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
阅卷员反馈了 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.

应该这样写:"视图是一条存储的 SELECT 查询,被呈现为一张虚拟表。它自己不持有数据,所以每次被查询时都读取实时的基础表。物化视图是例外:它存储预先计算好的结果集,并定期刷新。"先说存储查询这个思想,再说例外。

Revision questions

复习题

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

仿真题风格。先写出你的答案,再显示评分要点并给自己打分。

Question 1 · 2 marks第 1 题 · 2 分

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

[2]描述什么是数据库视图。

Question 2 · 4 marks第 2 题 · 4 分

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

[4]区分虚拟视图和物化视图。

Question 3 · 5 marks第 3 题 · 5 分

[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.

[5]一家酒店让客房部员工访问一个视图,而不是 GUEST 表本身。描述这个决定的两个好处,并解释为什么这个视图很可能是只读的。

Flashcards

闪卡

Active recall for this statement's key terms.

针对本条陈述关键术语的主动回忆。

Prototype render. Concepts (virtual views, simple and complex views, materialized snapshot views, the seven benefits, and the three conditions for an updatable view) grounded in the IB CS 2027 syllabus's A3.3.5 section; the hotel GUEST and STAY worked example is original, built to reinforce the same concepts with fresh data rather than reproduce a real course example’s own Employees and Department scenario. Not yet wired to live routes.