跳到主要内容

数据库

A-Level 计算机科学知识点 8

本主题的视频课程 打开视频页面
17:15

数据库与关系模型

在数据库之前,每个程序都保存自己的平面文件——每个程序对应一个文件。想象一家商店。销售程序、账单程序和运输程序……

英文讲解 · 可切换中英文字幕

8.1

数据库概念

教学大纲
考生应具备的能力 注释与指导
展示对使用基于文件的方法进行数据存储和检索的局限性的理解
描述关系型数据库中用于解决基于文件方法局限性的特性
展示对关系型数据库模型相关术语的理解并加以运用 包括实体、表、记录、字段、元组、属性、主键、候选键、次级键、外键、关系(一对多、一对一、多对多)、参照完整性、索引
使用实体-关系 (E-R) 图来记录数据库设计
展示对规范化过程的理解 第一范式 (1NF)、第二范式 (2NF) 和 第三范式 (3NF)
解释给定的数据库表集合为何是或不是符合 3NF 的
根据数据库的描述、给定的数据集合或给定的表集合,生成一个规范化的数据库设计

来源:Cambridge International 教学大纲

English

Before databases, programs stored data in flat files 平面文件 — usually one file per program. This is fine for small data but breaks down at scale.

A hand searching a card-index filing cabinet
File-based storage keeps data in separate files, like papers in a filing cabinet — hard to search and easy to duplicate

Limitations

  • data redundancy 数据冗余 — the same data (a customer's address) is held in several files, one per program, so storage is wasted and every copy must be updated.
  • data inconsistency 数据不一致 — when one copy is updated and another is not, the files disagree and nobody knows which is right.
  • data dependence — each program is written for the exact layout of its files; change a field's length or add a field and every program that reads the file must be rewritten.
  • no shared access — a file is locked while one program uses it, so users cannot work on the data at the same time.
  • weak integrity 完整性 — no central rules stop an invalid value or a link to a customer who does not exist; weak security — access is per file, not per field; and queries across files need a new program each time.
The Payroll and Sales programs each link to their own separate data file, so the Staff Number field is stored twice
The file-based approach: each program keeps its own files

A relational database 关系数据库 fixes these by storing data in tables managed by one piece of software (the DBMS) that all programs use.

One DBMS holding tables design, validation rules, access rights and the data, with a single shared database, used by both the payroll and sales applications
The database approach: one DBMS serves all the programs

Why a relational database is better — the three-mark answer. Each item of data is stored once, in one table, and tables are linked by keys, so there is no redundancy and no inconsistency; the data is independent of the programs, which ask the DBMS for what they need and are unaffected when the structure changes; and the DBMS enforces integrity rules, controls access per user and per field, allows many users at once, and answers any query without a new program being written.

Worked example. A repair shop stores its customers, devices and repair jobs using a file-based approach, one file per program. Give three problems this causes, and describe how a relational database would remove them.

The customer's name and phone number are stored in the repairs file and the invoices file (redundancy); when a customer changes number, one file is updated and the other is not (inconsistency); and when the shop wants a new report — repairs per technician — a new program has to be written to read the files (no ad-hoc queries). In a relational database the customer is stored once in a CUSTOMER table and referred to by CustomerID from the REPAIR table, so a change is made once and is seen everywhere; the report is a single SQL query.

词汇 训练
English 中文 拼音
flat files/flæt faɪlz/ 平面文件 píng miàn wén jiàn
data redundancy/ˈdeɪtə rɪˈdʌndənsi/ 数据冗余 shù jù rǒng yú
data inconsistency/ˈdeɪtə ˌɪnkənˈsɪstənsi/ 数据不一致 shù jù bù yī zhì
integrity/ɪnˈteɡrɪti/ 完整性 wán zhěng xìng
relational database/rɪˈleɪʃənl ˈdeɪtəbeɪs/ 关系数据库 guān xì shù jù kù
8.1

关系模型 —— 术语

English
  • table 表 (relation) — a grid of rows and columns; one table per type of entity 实体 (e.g. CUSTOMER).
  • record 记录 (row, also called a tuple 元组) — one row; one instance of the entity.
  • field 字段 (column, also called an attribute 属性) — one column; one piece of information about each record.
  • primary key 主键 — a field (or fields) that uniquely identifies each record; never null or duplicated.
  • foreign key 外键 — a field whose value matches the primary key of another table, linking the two.
  • composite key 复合键 — a primary key made of two or more fields together.
  • candidate key 候选键 — any field(s) that could be the primary key.
  • secondary key 次键 — a non-primary field that is indexed for fast searching.
  • indexing 索引 — building an index on a field so look-ups and joins run faster.
  • referential integrity 参照完整性 — every foreign-key value must match an existing primary key (no orphan records).
The Student table with each row labelled as a record, each column as a field, and the StudentID column highlighted as the primary key
Each row is a record and each column is a field; the primary key (StudentID) is unique for every record

A table is written in shorthand with the primary key underlined and foreign keys noted:

CUSTOMER(CustomerID, Name, Phone)
ORDER(OrderID, CustomerID, OrderDate)   -- CustomerID is FK → CUSTOMER
Two tables linked by a foreign key: the CUSTOMER table has primary key CustomerID; the ORDER table has its own primary key OrderID plus a CustomerID foreign key whose value matches a CustomerID in CUSTOMER
A foreign key links two tables: ORDER.CustomerID matches the primary key CUSTOMER.CustomerID

Worked example. State what is meant by entity, primary key and referential integrity in a relational database, and complete the term ↔ description table for tuple and attribute.

An entity is something about which data is stored — a person, object or event — which becomes one table. A primary key is the attribute (or combination of attributes) that uniquely identifies each record in a table. Referential integrity means that every foreign-key value must match the value of a primary key in the table it refers to, so a record cannot refer to one that does not exist. A tuple is one row of a table (one record); an attribute is one column (one field). Learn the pairs: table/relation, record/tuple, field/attribute.

探索

用 SELECT 读一个关系表

一个关系表只是行(记录)和列(字段)。WHERE 保留匹配一个条件的行;SELECT 然后只保留你要的列。

词汇 训练
English 中文 拼音
field/fiːld/ 字段 zì duàn
table/ˈteɪbl/ 表 biǎo
entity/ˈentɪti/ 实体 shí tǐ
record/ˈrekɔːd/ 记录 jì lù
tuple/ˈtuːpl/ 元组 yuán zǔ
attribute/ˈætrɪbjuːt/ 属性 shǔ xìng
primary key/ˈpraɪməri kiː/ 主键 zhǔ jiàn
foreign key/ˈfɒrən kiː/ 外键 wài jiàn
composite key/ˈkɒmpəzɪt kiː/ 复合键 fù hé jiàn
candidate key/ˈkændɪdeɪt kiː/ 候选键 hòu xuǎn jiàn
secondary key/ˈsekəndəri kiː/ 次键 cì jiàn
indexing/ˈɪndeksɪŋ/ 索引 suǒ yǐn
referential integrity/ˌrefəˈrenʃl ɪnˈteɡrɪti/ 参照完整性 cān zhào wán zhěng xìng
8.1

实体-关系(E-R)图

English

An entity-relationship diagram 实体关系图 shows the structure: each entity is a rectangle, each relationship a line, with the cardinality 基数 marked at each end:

  • one-to-one (1:1).
  • one-to-many 一对多 (1:M) — each Customer has many Orders; each Order has one Customer.
  • many-to-many (M:N) — Students take many Courses, and Courses have many Students.
An E-R diagram with a STUDENT entity and a CLASS entity joined by a relationship line, a crow's foot for many at the student end, and the line just ends at the class end for one
An E-R diagram: one class has many students
The two crow's-foot line ends: for one, the line just ends at the entity box; for many, a crow's foot touches the box
Crow's-foot symbols for the cardinality of a relationship

A many-to-many relationship cannot be stored directly. Break it into two one-to-many relationships through a link table 连接表 holding the two foreign keys:

ENROLMENT(StudentID, CourseID, EnrolmentDate)
A many-to-many relationship between STUDENT and COURSE stored as two one-to-many relationships through an ENROLMENT link table holding StudentID and CourseID
A link table resolves a many-to-many relationship into two one-to-many relationships

Drawing the E-R diagram for a given set of tables. Each table becomes an entity. A relationship can be implemented by a foreign key referring to another table. Determine its cardinality from the business rules and constraints: a non-unique foreign key can represent the many end of a one-to-many relationship, while a unique foreign key can represent a one-to-one relationship. A table with two foreign keys and no other identity is usually a link table resolving a many-to-many relationship. Label each line with the relationship type.

An E-R diagram for a repair-shop database with four entities: CUSTOMER one-to-many DEVICE, DEVICE one-to-many REPAIR and TECHNICIAN one-to-many REPAIR, with crow's-foot notation and the primary and foreign keys shown
Drawing the diagram from the tables: every foreign key is a one-to-many relationship, with the "many" at the table that holds it

Worked example. A repair shop has the tables CUSTOMER(CustomerID, Name, Phone), DEVICE(DeviceID, CustomerID, Type, Model), TECHNICIAN(TechnicianID, Name) and REPAIR(RepairID, DeviceID, TechnicianID, RepairDate, Cost). Identify the relationships and their types.

DEVICE holds CustomerID, so CUSTOMER–DEVICE is one-to-many (one customer, many devices). REPAIR holds DeviceID, so DEVICE–REPAIR is one-to-many; it also holds TechnicianID, so TECHNICIAN–REPAIR is one-to-many. There is no direct CUSTOMER–REPAIR line: the link runs through DEVICE. Three lines, three crow's feet, all at the REPAIR or DEVICE ends.

词汇 训练
English 中文 拼音
entity-relationship diagram/ˈentɪti rɪˈleɪʃənʃɪp ˈdaɪəɡræm/ 实体关系图 shí tǐ guān xì tú
cardinality/ˌkɑːdɪˈnælɪti/ 基数 jī shù
one-to-many/wʌn tə ˈmeni/ 一对多 yī duì duō
link table/lɪŋk ˈteɪbl/ 连接表 lián jiē biǎo
8.1

规范化

English

Normalisation 规范化 organises tables to cut redundancy and inconsistency, going through normal forms 范式 in order.

  • First normal form (1NF) — every field holds a single (atomic 原子) value, with no repeating groups, and a primary key.
  • Second normal form (2NF) — in 1NF, and every non-key field depends on the whole primary key (only matters for a composite key).
  • Third normal form (3NF) — in 2NF, and every non-key field depends only on the primary key, not on another non-key field (no transitive dependency 传递依赖).

A 3NF design stores each fact once, so insert/update/delete anomalies disappear. The trade-off is more tables and more joins. Aim for 3NF.

To produce a 3NF design: find the entities and their attributes; choose a primary key for each; split repeating/non-atomic fields (1NF); split fields depending on part of a composite key (2NF); split fields depending transitively on the key (3NF); add foreign keys for the relationships.

Normalisation: one table where the customer name and phone repeat on every order is split into a separate ORDER table and CUSTOMER table, so each fact is stored once
Normalisation removes redundancy by splitting repeated data into its own table

Worked example. The table ORDER(OrderID, CustomerID, CustomerName, ProductID, Quantity) has the composite primary key (OrderID, ProductID). Normalise it to 3NF. Test each non-key field against the key. Quantity depends on both OrderID and ProductID, which is fine. But CustomerID depends on OrderID alone - only part of the composite key. That is a partial dependency, so the table is not in 2NF. Split it into ORDER_LINE(OrderID, ProductID, Quantity) and ORDER(OrderID, CustomerID, CustomerName). Now test 3NF: in that new ORDER table, CustomerName depends on CustomerID, which is not the key - a transitive dependency. Split again: ORDER(OrderID, CustomerID) and CUSTOMER(CustomerID, CustomerName). Name the dependency that breaks each form (partial breaks 2NF, transitive breaks 3NF); "it has repeated data" describes the symptom and earns nothing.

The three questions to ask of any table. Is every cell a single value, with no repeating group? If not, it is not in 1NF. If the key is composite, does every non-key field depend on the whole key? If some field depends on part of it, there is a partial dependency 部分依赖 and the table is not in 2NF. Does every non-key field depend on the key alone? If a field depends on another non-key field, there is a transitive dependency and the table is not in 3NF. An "explain why the table is not in 3NF" answer names the dependency and the fields involved.

Normalising a car-rental table in three steps: the repeating group of cars is removed for 1NF, the car details that depend on CarReg alone are moved to a CAR table for 2NF, and the customer details that depend on CustomerID are moved to a CUSTOMER table for 3NF
1NF removes the repeating group, 2NF the partial dependency, 3NF the transitive dependency

Worked example. A car-rental shop records each rental as RENTAL(RentalID, RentalDate, CustomerID, CustomerName, CustomerPhone, CarReg, CarModel, DailyRate, Days), where one rental can include several cars. Explain why the table is not normalised and produce a 3NF design.

Not in 1NF: the car fields CarReg, CarModel, DailyRate, Days form a repeating group — one rental has several cars. Move them to RENTAL_CAR(RentalID, CarReg, CarModel, DailyRate, Days) with the composite key (RentalID, CarReg). Not in 2NF: in RENTAL_CAR, CarModel and DailyRate depend on CarReg alone — a partial dependency. Move them to CAR(CarReg, CarModel, DailyRate), leaving RENTAL_CAR(RentalID, CarReg, Days). Not in 3NF: in RENTAL, CustomerName and CustomerPhone depend on CustomerID, a non-key field — a transitive dependency. Move them to CUSTOMER(CustomerID, CustomerName, CustomerPhone), leaving RENTAL(RentalID, RentalDate, CustomerID). The 3NF design is four tables — CUSTOMER, RENTAL, RENTAL_CAR, CAR — with CustomerID, RentalID and CarReg as foreign keys; underline every primary key.

词汇 训练
English 中文 拼音
normalisation/ˌnɔːməlaɪˈzeɪʃn/ 规范化 guī fàn huà
normal forms/ˈnɔːml fɔːmz/ 范式 fàn shì
atomic/əˈtɒmɪk/ 原子 yuán zi
transitive dependency/ˈtrænsɪtɪv dɪˈpendənsi/ 传递依赖 chuán dì yī lài
partial dependency/ˈpɑːʃl dɪˈpendənsi/ 部分依赖 bù fèn yī lài
8.2

数据库管理系统(DBMS)

教学大纲
考生应能够: 注释与指导
展示对数据库管理系统(DBMS)所提供的、用于解决基于文件方法所面临问题的各项功能的理解 包括:• 数据管理,包括维护数据字典 • 数据建模 • 逻辑模式 • 数据完整性 • 数据安全,包括备份程序以及为个人/用户组使用访问权限的功能
展示对 DBMS 中软件工具在实际应用中的用法的理解 包括以下工具的使用和目的:• 开发者接口 • 查询处理器

来源:Cambridge International 教学大纲

English

A DBMS 数据库管理系统 manages the database centrally. Features that fix the file-based limits:

  • data dictionary 数据字典 — a description of every table, field, type and key; programs query it instead of hard-coding the structure.
  • redundancy/consistency control — each fact stored once.
  • concurrent access 并发访问 control — locks and transactions let many users work at once.
  • backup 备份 and recovery; security and per-user permissions.
  • integrity rules — keys, unique and range constraints, enforced centrally.
  • transactions 事务 — a group of operations that all succeed or all fail.
  • views 视图 — virtual tables that show each user "their" slice of the data.
  • data management 数据管理 and data modelling 数据建模 — control how data is stored and define its structure as a logical schema 逻辑模式 (the logical design, independent of physical storage).
  • data integrity 数据完整性 and data security 数据安全 — enforce correctness and control access centrally.
  • a query processor 查询处理器 runs queries; a developer interface 开发者接口 gives tools and APIs for building applications.

Its tools include a data-dictionary editor, a query builder, a forms builder, a report generator, user management, and an SQL editor.

What the data dictionary holds (a "give three items" question): the names of the tables; the names of the fields in each table; each field's data type and length; the primary and foreign keys and the relationships between tables; validation rules; indexes; and who may access each table. It is metadata — data about the data — and the DBMS uses it to check every query and every change.

How the DBMS keeps the data secure (a "describe two methods" question): authentication 身份验证 — a username and password, or a biometric, before any access; access rights — each user or group is allowed to read, write or delete only certain tables or fields, often through a view; encryption of the stored data and of data sent to it, so a copied file is unreadable; backups taken regularly, so the data can be restored after loss; and a transaction log that records who changed what.

The two software tools. The developer interface is what a programmer uses to build the database and the applications on it: create tables and set keys and validation, write queries and SQL, and design forms and reports, without knowing how the data is physically stored. The query processor takes a query (SQL from a program, or a query built in the interface), checks it against the data dictionary, works out the most efficient way to run it, retrieves the data and returns the results.

Logical schema. The DBMS keeps the logical design (which tables and fields exist and how they relate) separate from the physical storage (files, indexes, disk blocks). Programs work with the logical schema, so the physical storage can be reorganised without changing a single program — this is the data independence the file-based approach lacked.

A hard disk drive with its cover removed, showing the stacked mirror-like platters and the read/write head arm resting over them
The physical storage the logical schema hides: a hard disk's spinning platters and read/write head
探索

数据库服务实验

观看 DBMS 如何将查询转化为安全的共享数据访问。

探索

数据库服务实验

观看 DBMS 如何将查询转化为安全的共享数据访问。

词汇 训练
English 中文 拼音
DBMS/ˌdiː biː em ˈes/ 数据库管理系统 shù jù kù guǎn lǐ xì tǒng
data dictionary/ˈdeɪtə ˈdɪkʃənəri/ 数据字典 shù jù zì diǎn
concurrent access/kənˈkʌrənt ˈækses/ 并发访问 bìng fā fǎng wèn
transactions/trænˈsækʃnz/ 事务 shì wù
backup/ˈbækʌp/ 备份 bèi fèn
views/vjuːz/ 视图 shì tú
data management/ˈdeɪtə ˈmænɪdʒmənt/ 数据管理 shù jù guǎn lǐ
data modelling/ˈdeɪtə ˈmɒdəlɪŋ/ 数据建模 shù jù jiàn mó
logical schema/ˈlɒdʒɪkl ˈskiːmə/ 逻辑模式 luó jí mó shì
data integrity/ˈdeɪtə ɪnˈteɡrɪti/ 数据完整性 shù jù wán zhěng xìng
data security/ˈdeɪtə sɪˈkjʊərɪti/ 数据安全 shù jù ān quán
query processor/ˈkwɪərɪ ˈprəʊsesə/ 查询处理器 chá xún chǔ lǐ qì
developer interface/dɪˈveləpə ˈɪntəfeɪs/ 开发者接口 kāi fā zhě jiē kǒu
authentication/ɔːˌθentɪˈkeɪʃn/ 身份验证 shēn fèn yàn zhèng
8.3

数据定义语言(DDL)与数据操纵语言(DML)

教学大纲
考生应具备的能力 注释与指导
展示对 DBMS 使用其 数据定义语言 (DDL) 执行数据库结构所有创建/修改的理解
展示对 DBMS 使用其 DML 执行所有查询和数据维护的理解
展示对 DDL 和 DML 的行业标准均为 结构化查询语言 (SQL) 的理解 理解给定的 SQL 语句
理解给定的 SQL (DDL) 语句,并能使用语句子集编写简单的 SQL (DDL) 语句 创建数据库 (CREATE DATABASE);创建表定义 (CREATE TABLE),包括具有合适数据类型的属性创建:• CHARACTER • VARCHAR(n) • BOOLEAN • INTEGER • REAL • DATE • TIME;更改表定义 (ALTER TABLE);向表中添加主键 (PRIMARY KEY (字段));向表中添加外键 (FOREIGN KEY (字段) REFERENCES 表名 (字段))
编写用于查询或修改存储在(最多两个)数据库表中的数据 (DML) 的 SQL 脚本 查询包括 SELECT... FROM、WHERE、ORDER BY、GROUP BY、INNER JOIN、SUM、COUNT、AVG
数据维护包括 INSERT INTO、DELETE FROM、UPDATE

来源:Cambridge International 教学大纲

English

SQL 结构化查询语言 (Structured Query Language) has two halves:

SQL splits into DDL (builds the structure) and DML (works with the data)
DDL builds the database structure; DML works with the data
  • Data Definition Language 数据定义语言 (DDL) — creates or changes the structure (tables, keys, constraints).
  • Data Manipulation Language 数据操纵语言 (DML) — works with the data (insert, update, delete, query 查询).

DDL basics

CREATE TABLE CUSTOMER (
  CustomerID INTEGER PRIMARY KEY,
  Name VARCHAR(50) NOT NULL,
  Phone VARCHAR(20)
);

Add a foreign key:

CREATE TABLE ORDER (
  OrderID INTEGER PRIMARY KEY,
  CustomerID INTEGER,
  OrderDate DATE,
  FOREIGN KEY (CustomerID) REFERENCES CUSTOMER(CustomerID)
);

Modify and drop:

ALTER TABLE CUSTOMER ADD Email VARCHAR(100);
DROP TABLE CUSTOMER;

Common types: INTEGER, REAL, VARCHAR(n), CHAR(n) (also CHARACTER(n)), DATE, TIME, BOOLEAN, DECIMAL(p, s).

DML basics

Query with SELECT:

A SELECT query returns just the rows that match its condition
A SELECT query returns only the rows that match its condition
SELECT Name, Phone
FROM CUSTOMER
WHERE Name LIKE 'A%'
ORDER BY Name ASC;

SELECT lists fields, FROM names the table, WHERE filters rows, ORDER BY sorts.

A join 连接 combines two tables using a foreign-key relationship:

SELECT C.Name, O.OrderDate
FROM CUSTOMER C INNER JOIN ORDER O
  ON C.CustomerID = O.CustomerID
WHERE O.OrderDate >= '2024-01-01';
An SQL query annotated line by line: SELECT names the fields and a COUNT column, FROM names the first table with an alias, INNER JOIN ON links the second table through the foreign key, WHERE keeps matching rows, GROUP BY makes one row per customer, ORDER BY sorts the result
The parts of a query, in the order they must be written

Aggregate functions 聚合函数 (COUNT, SUM, AVG, MIN, MAX) are often used with GROUP BY:

SELECT CustomerID, COUNT(*) AS NumOrders
FROM ORDER
GROUP BY CustomerID;

Insert, update, delete:

INSERT INTO CUSTOMER (CustomerID, Name, Phone)
VALUES (101, 'Ada Lovelace', '020-1234-5678');

UPDATE CUSTOMER SET Phone = '020-9999-0000' WHERE CustomerID = 101;

DELETE FROM CUSTOMER WHERE CustomerID = 101;

Always put a WHERE clause on UPDATE and DELETE, or the change hits every row.

Tips for exam SQL

  • use the exact table and field names from the question.
  • quote strings with single quotes ('Smith'); don't quote numbers.
  • comparisons: =, <, >, <=, >=, <>.
  • LIKE 'A%' matches anything starting with A (% = any string, _ = one character); IN (1,2,3); BETWEEN 10 AND 20.
  • combine conditions with AND / OR / NOT, and end each statement with a semicolon.

The DDL pattern the exam wants. Every CREATE TABLE names each field with its type, marks the primary key, and declares each foreign key with the table it references; a composite key is declared on its own line:

CREATE TABLE RENTAL_CAR (
  RentalID INTEGER,
  CarReg VARCHAR(8),
  Days INTEGER,
  PRIMARY KEY (RentalID, CarReg),
  FOREIGN KEY (RentalID) REFERENCES RENTAL(RentalID),
  FOREIGN KEY (CarReg) REFERENCES CAR(CarReg)
);

Worked example. Using CUSTOMER(CustomerID, Name, Phone) and DEVICE(DeviceID, CustomerID, Type, Model), write SQL scripts to: (a) list the name and phone number of every customer who owns a device of type 'tablet', in alphabetical order of name; (b) count the devices of each type; (c) record that customer 17 now has the phone number '0771 234 5678'; (d) add a new device, ID 305, a 'laptop' of model 'X1' belonging to customer 17.

(a)

SELECT CUSTOMER.Name, CUSTOMER.Phone
FROM CUSTOMER INNER JOIN DEVICE
  ON CUSTOMER.CustomerID = DEVICE.CustomerID
WHERE DEVICE.Type = 'tablet'
ORDER BY CUSTOMER.Name ASC;

(b)

SELECT Type, COUNT(DeviceID) AS NumberOfDevices
FROM DEVICE
GROUP BY Type;

(c) UPDATE CUSTOMER SET Phone = '0771 234 5678' WHERE CustomerID = 17; (d) INSERT INTO DEVICE (DeviceID, CustomerID, Type, Model) VALUES (305, 17, 'laptop', 'X1');

Marks are given per clause — the fields, the tables, the join condition, the WHERE, the ORDER BY — so a script with one wrong clause still scores the rest. Write Table.Field whenever two tables are involved.

Worked example. Explain what this script does: SELECT T.Name, SUM(R.Cost) AS Total FROM TECHNICIAN T INNER JOIN REPAIR R ON T.TechnicianID = R.TechnicianID GROUP BY T.Name;

It outputs each name represented among technicians with repairs, with the total cost of repairs by technicians sharing that name. The inner join excludes technicians with no repairs, and grouping by name combines technicians who have the same name. When asked what a script does, describe the result, not the syntax.

SQL conditions and keys

LIKE 'Li%' matches text starting with Li; % represents any number of characters and _ exactly one. AND requires both conditions; OR requires at least one. Use brackets when combining them.

CREATE DATABASE Club;
CREATE TABLE BOOKING (
  MemberID INTEGER,
  EventID INTEGER,
  PRIMARY KEY (MemberID, EventID)
);
ALTER TABLE BOOKING ADD FOREIGN KEY (MemberID)
  REFERENCES MEMBER(MemberID);
SELECT Name FROM MEMBER
WHERE Name LIKE 'Li%' AND (Year = 12 OR Year = 13)
ORDER BY Name DESC;

The composite primary key makes the pair unique, not each column separately. A foreign key prevents a non-null reference to a missing parent record. ON UPDATE CASCADE carries a parent-key change to referencing rows; ON DELETE CASCADE deletes those rows when the parent is deleted.

探索

用 INNER JOIN 缝合两个表

一个连接匹配外键等于主键的行——这里 Orders.CustomerID = Customer.CustomerID——并把每个匹配的对组合成一个更宽的行。

探索

SELECT … WHERE

逐步执行查询:WHERE 保留匹配的行,然后 SELECT 挑选你请求的列。

词汇 训练
English 中文 拼音
query/ˈkwɪərɪ/ 查询 chá xún
SQL/ˌes kjuː ˈel/ 结构化查询语言 jié gòu huà chá xún yǔ yán
join/dʒɔɪn/ 连接 lián jiē
Data Definition Language/ˈdeɪtə ˌdefɪˈnɪʃn ˈlæŋɡwɪdʒ/ 数据定义语言 shù jù dìng yì yǔ yán
Data Manipulation Language/ˈdeɪtə məˌnɪpjʊˈleɪʃn ˈlæŋɡwɪdʒ/ 数据操纵语言 shù jù cāo zòng yǔ yán
aggregate functions/ˈæɡrɪɡeɪt ˈfʌŋkʃnz/ 聚合函数 jù hé hán shù
8.3

考官认可的定义

English

A definition question is marked against fixed wording. Learn these exactly, and give one answer only.

Term Definition
entity something about which data is stored — a person, object or event — which becomes a table in a relational database
attribute one item of data about an entity (a column of the table)
tuple one row of a table: one instance of the entity
primary key an attribute, or combination of attributes, that uniquely identifies each record in a table
foreign key an attribute in one table whose value matches a primary key in another table, used to link the two
candidate key any attribute (or combination) that could be chosen as the primary key
secondary key a non-primary attribute that is indexed so the table can be searched or sorted on it quickly
composite key a primary key made of two or more attributes together
referential integrity every foreign-key value must match an existing primary-key value in the table it refers to
first normal form a table in which every attribute is atomic, there are no repeating groups, and there is a primary key
second normal form in 1NF, and every non-key attribute depends on the whole of the primary key (no partial dependency)
third normal form in 2NF, and no non-key attribute depends on another non-key attribute (no transitive dependency)
data dictionary the metadata a DBMS keeps about the structure of the database: tables, fields, types, keys, relationships, validation
DDL / DML the language used to define or change the structure of a database / the language used to query and maintain the data in it
8.3

考试技巧

English
  • Define the terms exactly: entity, attribute, primary key, foreign key, and the relationship types (1:1, 1:many, many:many).
  • Give a reason at each normal form: 1NF (no repeating groups), 2NF (no partial dependency), 3NF (no non-key dependency) — and name the fields involved.
  • Explain what a DBMS provides (data independence, security, integrity, concurrent access, a data dictionary, a developer interface, a query processor).
  • Distinguish DDL (define the structure) from DML (query and change the data), and write SQL clause by clause: SELECT, FROM, INNER JOIN … ON, WHERE, GROUP BY, ORDER BY.
  • To draw an E-R diagram from tables, find each foreign key first, then use the constraints and business rules to determine the relationship cardinality.

Common mistakes

  • Drawing a many-to-many relationship directly. It must be split into two one-to-many relationships through a link table holding both foreign keys.
  • Explaining "not in 3NF" by "the data is repeated". Name the dependency (partial or transitive) and the fields involved.
  • Double quotes round strings in SQL, or quotes round numbers. Strings take 'single quotes'; numbers take none.
  • Leaving out the ON condition after INNER JOIN. Without it the two tables are not linked.
  • Putting an ordinary field next to COUNT or SUM in a SELECT without a GROUP BY.
  • UPDATE or DELETE without a WHERE. It changes or removes every row in the table.

该知识点的互动课程

逐步完成,配合即时检查练习。

历年真题

更多 A-Level 计算机科学 知识点

IGCSE, A-Level & AP