Skip to content

University Computer Science Topic 8 6:35 English narration · English and 中文 subtitles you can switch

space play · ←/→ 5s · j/l 10s · f fullscreen · c subtitles · ,/. speed

Chapters

Transcript
A database is a set of true sentences about a domain. 数据库是关于某个领域的一组真实陈述。
This course shows how to model, query, protect and govern data. 这门课介绍如何建模、查询、保护和治理数据。
The first three units model and shape it: relational design, SQL and normalisation. 前三个单元介绍如何建模和组织数据:关系设计、SQL 和规范化。
The next two make it fast and safe, with indexes and transactions. 接下来的两个单元介绍如何利用索引和事务,让数据访问快速且安全。
The last governs it. 最后一个单元介绍数据治理。
Every query here was executed against the documented sample schema. 这里的每条查询都已在文档记录的示例模式上执行过。
Entities become tables, and relationships become keys. 实体变成表,关系变成键。
Students, courses and departments are entities. 学生、课程和院系都是实体。
A student enrols in many courses and a course has many students, so that many-to-many relationship becomes an Enrollment table of foreign-key pairs. 一个学生选修多门课程,一门课程也有多个学生,因此这种多对多关系变成一个由外键对组成的 Enrollment 表。
Its primary key of student, course and term rejects a duplicate enrolment inside the engine itself. 由学生、课程和学期组成的主键,会在数据库引擎内部拒绝重复选课记录。
Rules live in the schema. 规则保存在数据库模式中。
Follow the arrows on this entity-relationship diagram and name each cardinality. 沿着这张实体关系图中的箭头,指出每个关系的基数。
Each enrolment row points to exactly one student and one course, while each student and each course can have many enrolments. 每条选课记录都恰好指向一个学生和一门课程,而每个学生和每门课程都可以对应多条选课记录。
The junction table turns one many-to-many relationship into two simple one-to-many links that foreign keys can enforce. 连接表把一个多对多关系转换成两个简单的一对多关系,使外键能够强制执行这些关系。
A student takes many courses, and a course has many students. 一个学生选修多门课程,一门课程也有多个学生。
The enrolments table resolves this: each row holds one student ID and one course ID, both foreign keys. 选课表解决了这个问题:每一行包含一个学生 ID 和一个课程 ID,两者都是外键。
Student 1 takes courses 10 and 11, and student 2 takes course 10. 学生 1 选修课程 10 和 11,学生 2 选修课程 10。
Read every query as a pipeline. 把每条查询看作一条流水线。
FROM builds the rows, WHERE filters them, GROUP BY makes groups, HAVING filters the groups, SELECT picks columns and ORDER BY sorts. FROM 构建行,WHERE 过滤行,GROUP BY 分组,HAVING 过滤组,SELECT 选取列,ORDER BY 排序。
A plain join drops the two courses with nobody enrolled; a LEFT JOIN keeps them with a count of zero. 普通连接会丢掉没有人选修的两门课程;LEFT JOIN 会保留它们,计数为零。
COUNT of a column skips NULLs, but COUNT star does not. 对某一列使用 COUNT 时会跳过 NULL,但 COUNT(*) 不会。
Name each stage before you read the SQL. 阅读 SQL 之前,先说出每个阶段的名称。
Rows flow left to right through the five operators of relational algebra: a join combines tables, selection keeps matching rows, grouping partitions them, a filter on groups keeps the large ones, and projection keeps the wanted columns. 行从左向右流经关系代数的五个运算:连接合并表,选择保留匹配的行,分组对行进行划分,组过滤保留较大的组,投影保留所需的列。
Here the result is one row: Databases, with three students. 这里的结果是一行:Databases,有三个学生。
SQL does not run in the order it is written. SQL 的执行顺序与书写顺序不同。
FROM and JOIN gather all the rows. FROM 和 JOIN 汇集所有行。
WHERE drops individual rows. WHERE 剔除单独的行。
GROUP BY forms the groups. GROUP BY 形成各个组。
HAVING drops whole groups, so it can test aggregates such as COUNT. HAVING 剔除整个组,因此它可以检验 COUNT 等聚合结果。
SELECT returns the result. SELECT 返回结果。
Anomalies are symptoms; the disease is a fact stored more than once. 异常是症状;根源是同一个事实被存储了多次。
One wide enrolment table repeats each course's title, department and building. 一张宽选课表会重复记录每门课程的名称、院系和教学楼。
Course decides title, which depends on only part of the key; title decides department, and department decides building, a chain. 课程决定名称,而名称只依赖于键的一部分;名称决定院系,院系决定教学楼,形成一条依赖链。
Splitting by these dependencies gives three tables and no anomalies. 根据这些依赖拆分,可以得到三张表并消除异常。
Denormalise only on evidence. 只有在有证据支持时才进行反规范化。
Point at each anomaly in the wide table. 指出宽表中的每一种异常。
Update a building in one row but not another, and the data contradicts itself. 一行更新了教学楼,另一行却没有更新,数据就会自相矛盾。
Delete the last student on a course, and the course's department disappears too. 删除某门课程的最后一个学生,该课程的院系信息也会消失。
Now split the table by its dependencies into three smaller ones, and watch each anomaly vanish, because each fact is stored only once. 现在根据依赖关系把表拆成三张较小的表,可以看到每种异常都消失了,因为每个事实只存储一次。
In the single table, the customer's name and phone number are repeated on every order. 在这张单表中,客户的姓名和电话号码在每个订单上都重复出现。
If the phone number changes, every copy must change. 如果电话号码改变,每一份副本都必须更改。
Normalisation splits the table into ORDER and CUSTOMER, linked by the customer ID, so each fact is stored once. 规范化将表拆分为 ORDER 和 CUSTOMER,通过客户 ID 连接,这样每个事实只存储一次。
A B-tree trades fan-out for height. B 树通过增加分支数来降低高度。
With a fan-out of 100, three levels cover a million keys, so any key takes three reads. 分支数为 100 时,三层可以覆盖一百万个键,因此查找任何键都需要三次读取。
The query plan tells the truth: without an index, the engine scans the whole Course table; with an index on credits, it searches the index. 查询计划会告诉我们实际情况:没有索引时,引擎扫描整个 Course 表;在 credits 上建立索引后,引擎就会搜索索引。
But every index adds a write to every insert. 但每增加一个索引,每次插入就会增加一次写入。
On the left, a B-tree of order 4 holds 15 keys in just two levels, so any key is found in two reads. 左边是一棵 4 阶 B 树,只用两层就容纳了 15 个键,因此查找任何键都只需两次读取。
On the right is the query plan that uses it: a search through the index, not a scan of the table. 右边是使用该索引的查询计划:它搜索索引,而不是扫描表。
Read the plan, not the query, to learn what the engine is actually doing. 要了解引擎实际在做什么,应当阅读查询计划,而不是查询语句。
A transaction is all or nothing: atomic, consistent, isolated and durable. 事务要么全部完成,要么全部不完成:它具有原子性、一致性、隔离性和持久性。
Without isolation, two transactions can both read a balance of 100. 没有隔离性时,两个事务可能都读取到 100 的余额。
One withdraws 10 and writes 90; the other deposits 20 and writes 120, and the withdrawal vanishes. 一个取出 10 并写入 90;另一个存入 20 并写入 120,于是取款记录就消失了。
Two-phase locking holds every lock until commit, so the result matches some serial order. 两阶段锁定将所有锁保持到提交,因此结果与某种串行执行顺序一致。
Deadlocks are resolved by retrying. 死锁通过重试来解决。
Same operations, two verdicts. 相同的操作,两种结果。
In the first interleaving, T1 reads, T2 reads, T2 writes 120, then T1 writes 90: the deposit is lost. 在第一种交错执行中,T1 读取,T2 读取,T2 写入 120,然后 T1 写入 90:存款丢失了。
In the second, T1 reads and writes 90 before T2 reads it and writes 110, exactly as if they ran one after the other. 在第二种情况下,T1 读取并写入 90,之后 T2 读取这个值并写入 110,结果与两者依次执行完全一样。
Isolation levels name which anomalies you are willing to accept. 隔离级别说明你愿意接受哪些异常。
Never paste user text into SQL. 绝不要将用户输入的文本直接拼接到 SQL 中。
Typing quote OR 1 equals 1 into a pasted query makes it match every row, all eight students. 在拼接的查询中输入单引号 OR 1 等于 1,会使它匹配每一行,也就是全部八个学生。
Parameterised statements end the whole class of attack. 参数化语句可以杜绝这一整类攻击。
Grant each role only what it needs: a reader may only select, a clerk may also insert enrolments. 只向每个角色授予所需的权限:读取者只能查询,办事员还可以插入选课记录。
Governance classifies data, assigns owners, sets retention and audits access. 数据治理负责对数据分类、指定负责人、设定保留期限并审计访问。
Every arrow on this access model is authority. 这个访问模型中的每个箭头都代表权限。
Grants flow from the owner to roles, and from roles to users, and nothing else flows. 权限从所有者授予角色,再从角色授予用户,没有其他授权路径。
If a session is stolen, the damage is bounded by its grants. 如果会话被盗,损失会受其权限限制。
Revoke grants when someone's role changes, and never let the application connect as the owner. 当某人的角色改变时,要撤销相应权限,绝不要让应用程序以所有者身份连接。
Count the arrows you really need. 数一数你真正需要多少个箭头。
Now test yourself from memory. 现在凭记忆测试一下自己。
What does a many-to-many relationship become in a relational schema? 在关系模式中,多对多关系会变成什么?
Which clause filters on COUNT star? 哪个子句根据 COUNT(*) 的结果进行过滤?
How many keys can a B-tree with fan-out 100 cover at height 3? 分支数为 100、高度为 3 的 B 树可以覆盖多少个键?
Where does the schedule R1, R2, W2, W1 leave the balance? 执行顺序 R1、R2、W2、W1 最终会使余额变成多少?
Pause, answer, then find it on the figure. 暂停,回答,然后在图中找到答案。

Comments

    Log in or create account

    IGCSE, A-Level & AP