A3.3.1 · Database programming
A3.3.1 · 数据库编程
The two data language types within SQL
SQL 中的两种数据语言类型
Building a shelf is not the same job as putting books on it, SQL keeps those two jobs in two separate language types, and knowing which is which is the whole of this statement.
造书架和往书架上放书不是同一件工作,SQL 把这两件工作放在两种不同的语言类型里,而知道哪个是哪个,就是这条陈述的全部。
Explainer
讲解
SQL isn't one single kind of instruction, it's really two different jobs wearing the same syntax. Some SQL statements shape the database itself, what tables exist, what columns they have. Other SQL statements never touch that shape at all, they only work with the data sitting inside it. Mixing the two up is easy, since both can sound like "changing" something.
SQL 并不是单一一种指令,它其实是用同样语法做的两种不同工作。有些 SQL 语句塑造数据库本身,有哪些表、表有哪些列。另一些 SQL 语句则完全不碰这个形状,只处理里面的数据。两者很容易混淆,因为听起来都像是在"改变"某样东西。
Data definition language (DDL) is the half of SQL that never touches a single row of data. Every DDL statement is really doing one of three things to a container, tables, views, indexes, constraints: bringing it into existence, reshaping it, or getting rid of it entirely.
数据定义语言(DDL)是 SQL 中从不触碰任何一行数据的那一半。每条 DDL 语句其实都是对一个容器(表、视图、索引、约束)做三件事之一:让它出现、改造它,或把它彻底去掉。
Data manipulation language (DML) is everything else, the half that never redraws the container but does whatever it wants with what's inside it: reading rows back out, adding new ones, changing what's there, or taking rows away. The table it's working in has to already exist, built by DDL first.
数据操纵语言(DML)是其余的一切,这一半从不重画容器,但对里面的内容为所欲为:把行读取出来、添加新行、改变已有的内容,或把行拿走。它所操作的表必须已经存在,先由 DDL 建好。
DDL statements
DDL 语句
| Statement | 语句 | What it does | 作用 |
|---|---|---|---|
| CREATE | Creates a new database object: a table, a view, an index. | 创建一个新的数据库对象:表、视图、索引。 | |
| PRIMARY KEY | Sets a field as a table's primary key. | 把一个字段设为表的主键。 | |
| FOREIGN KEY ... REFERENCES ... | Sets a field as a foreign key, naming the table and field it references. | 把一个字段设为外键,并指明它引用的表和字段。 | |
| ALTER | Reshapes a table that already has data in it: a column can be added or dropped, a constraint tightened, all without touching the rows already stored. | 改造一张已经有数据的表:可以添加或删除列、收紧约束,都不会触碰已经存储的行。 | |
| DROP | Deletes a database object entirely: a table, an index, a view. | 彻底删除一个数据库对象:表、索引、视图。 |
DML statements
DML 语句
| Statement | 语句 | What it does | 作用 |
|---|---|---|---|
| SELECT | Reads rows back out, from a single table or several joined together. | 把行读取出来,可以来自一张表,也可以来自连接在一起的几张表。 | |
| INSERT | Adds a brand-new row where none existed before. | 在原本没有的地方添加一个全新的行。 | |
| UPDATE | Changes the values sitting inside a row that is already there. | 改变已经存在的某一行里的值。 | |
| DELETE | Takes a row out of the table entirely. | 把一行从表中彻底拿走。 |
Worked example
例题讲解
An animal shelter's PET table: shape it, then fill it
一个动物收容所的 PET 表:先塑造,再填充
Click through and watch DDL build the table's structure first, then DML take over and work entirely inside it.
点击查看 DDL 先建立表的结构,然后 DML 接手,完全在其内部工作。
Check your understanding
检查你的理解
Which statement is DDL: ALTER TABLE PET ADD COLUMN Adopted BOOLEAN; or UPDATE PET SET Adopted = TRUE WHERE PetID = 4;?
哪一条语句是 DDL:ALTER TABLE PET ADD COLUMN Adopted BOOLEAN; 还是 UPDATE PET SET Adopted = TRUE WHERE PetID = 4;?
Common mistake
常见错误
A3.3.1A3.3.1 的这一常见模式RecurringBoth DDL and DML statements can sound like "changing" the database in everyday language, ALTER changes a table, UPDATE changes a record, and candidates often classify by that word alone. The real test is WHAT is changing: the structure itself (DDL), or a value already living inside an unchanged structure (DML).
反复出现在日常语言里,DDL 和 DML 语句听起来都像是在"改变"数据库,ALTER 改变一张表,UPDATE 改变一条记录,考生常常只凭这个词来分类。真正的检验标准是什么在变:是结构本身(DDL),还是已经存在于不变结构中的一个值(DML)。
What a weak answer looks like: "UPDATE is DDL because it changes the database." True that it changes something, but it changes a stored VALUE, not the table's structure, that makes it DML.
较弱的答案是这样的:"UPDATE 是 DDL,因为它改变了数据库。"它确实改变了某样东西,但改变的是一个存储的值,而不是表的结构,这使它成为 DML。
What it should say: "UPDATE is DML: it modifies a value already stored in an existing row, the table's columns and structure stay exactly as they were before and after." Names the type AND explains why by pointing at what actually changed.
应该这样写:"UPDATE 是 DML:它修改已存在的一行中已经存储的值,表的列和结构在前后保持完全不变。"点出了类型,并通过指出实际改变的是什么来解释原因。
Linking question
联系性问题
How does database programming in SQL differ from programming computationally in a high-level language?
SQL 中的数据库编程与用高级语言进行计算式编程有什么不同?
SQL statements like CREATE, SELECT or UPDATE describe WHAT result is wanted, the DBMS works out how to do it. A high-level language like Python or Java describes HOW to get there, step by step, in an explicit sequence the programmer controls directly. DDL and DML are exactly this declarative style in action, no loop or condition needed to add a record or change a table.
像 CREATE、SELECT 或 UPDATE 这样的 SQL 语句描述的是想要什么结果,由 DBMS 去决定怎么做。像 Python 或 Java 这样的高级语言描述的是怎样一步一步到达,按程序员直接控制的明确顺序执行。DDL 和 DML 正是这种声明式风格的体现,添加一条记录或修改一张表不需要任何循环或条件。
Does database programming in SQL require computational thinking?
SQL 中的数据库编程需要计算思维吗?
Yes, just not in the step-by-step form B2's own programming statements teach it in. Writing a correct query still means decomposing a request into the right combination of clauses, recognising the same filtering or grouping pattern across different questions, treating a table as an abstraction over whatever rows actually exist in it, and sequencing SELECT/FROM/WHERE/GROUP BY/HAVING correctly to reach a working result. The thinking is the same four skills, decomposition, pattern recognition, abstraction, algorithm design, expressed declaratively instead of imperatively.
需要,只是不是 B2 自己的编程陈述所教的那种一步一步的形式。写出一个正确的查询,仍然意味着把一个请求分解成正确的子句组合,在不同问题中识别相同的筛选或分组模式,把一张表当作对其中实际存在的行的抽象,并把 SELECT/FROM/WHERE/GROUP BY/HAVING 排列正确的顺序,以得到可行的结果。思考用到的还是那四项技能:分解、模式识别、抽象、算法设计,只是用声明式而不是命令式表达出来。
Revision questions
复习题
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
仿真题风格。先写出你的答案,再显示评分要点并给自己打分。
[2]Outline the difference between DDL and DML.
[2]概述 DDL 与 DML 的区别。
Marking points
评分要点
- DDL (data definition language) defines or changes the structure of the database: CREATE, ALTER, DROP.
- DML (data manipulation language) works with the rows inside that structure: SELECT, INSERT, UPDATE, DELETE.
- DDL(数据定义语言)定义或更改数据库的结构:CREATE、ALTER、DROP。
- DML(数据操作语言)处理该结构内部的行:SELECT、INSERT、UPDATE、DELETE。
[3]State whether each of the following is DDL or DML, with a reason: DROP TABLE Pet;, INSERT INTO Pet VALUES (5, 'Milo', 'Cat', 2);, CREATE INDEX idx_species ON Pet (Species);
[3]说明下列每一条是 DDL 还是 DML,并给出理由:DROP TABLE Pet;、INSERT INTO Pet VALUES (5, 'Milo', 'Cat', 2);、CREATE INDEX idx_species ON Pet (Species);
Marking points
评分要点
- DROP TABLE Pet; is DDL: it removes a table, changing the database structure.
- INSERT INTO Pet VALUES (...); is DML: it adds a row of data, leaving the structure unchanged.
- CREATE INDEX idx_species ON Pet (Species); is DDL: an index is a structural object added to the database.
- DROP TABLE Pet; 是 DDL:它移除一张表,改变数据库结构。
- INSERT INTO Pet VALUES (...); 是 DML:它添加一行数据,结构不变。
- CREATE INDEX idx_species ON Pet (Species); 是 DDL:索引是加入数据库的结构对象。
[4]A developer writes ALTER TABLE Pet ADD FOREIGN KEY (ShelterID) REFERENCES Shelter (ShelterID);. Explain why this is DDL rather than DML, and outline what would happen if a matching ShelterID didn't exist.
[4]一位开发者写了 ALTER TABLE Pet ADD FOREIGN KEY (ShelterID) REFERENCES Shelter (ShelterID);。解释为什么这是 DDL 而不是 DML,并概述如果不存在匹配的 ShelterID 会发生什么。
Marking points
评分要点
- ALTER TABLE changes the definition of the table (it adds a constraint), so it is DDL, not DML.
- It does not add, change or remove any rows of data.
- If existing ShelterID values in Pet did not match any Shelter row, the database would reject the ALTER because referential integrity would be violated.
- The developer would first have to fix or remove those rows, then re-run the statement.
- ALTER TABLE 改变表的定义(它增加一个约束),所以是 DDL,不是 DML。
- 它不添加、更改或移除任何数据行。
- 如果 Pet 中已有的 ShelterID 值没有匹配任何 Shelter 行,数据库会拒绝该 ALTER,因为会违反参照完整性。
- 开发者必须先修正或移除那些行,再重新执行该语句。
Flashcards
闪卡
Active recall for this statement's key terms.
针对本条陈述关键术语的主动回忆。
Keep this statement's practice with you
把本条陈述的练习带在身边
Free account: unlock every revision question and flashcard for A3.3.1, and every other IB CS 2027 statement.
免费账号:解锁 A3.3.1 以及其他所有 IB CS 2027 陈述的全部复习题和闪卡。