Databases and SQL
Python for IGCSE CS Lesson 13 2:09 English narration · English + 中文 subtitles burned in
Chapters
Transcript
A database keeps data in tables, and a table looks like a spreadsheet: rows and columns.
数据库把数据放在表里,而表看起来就像电子表格:有行有列。
Each row holds everything about one thing — one student here — and it is called a record.
每一行装着关于某一样东西的全部信息——这里是一名学生—— 它被称为一条记录。
Each column holds one piece of data about every student, and it is called a field.
每一列装着所有学生身上的同一项数据,它被称为一个字段。
SQL is the language you use to ask this table a question, and this lesson runs real queries against a real table.
SQL 就是你用来向这张表提问的语言, 而这一课会对一张真实的表运行真实的查询。
A query starts with SELECT, which chooses the columns you want.
查询以 SELECT 开头,它挑选你想要的列。
Ask for name, and only that column comes back — the ages and the grades are still in the table, they are simply not in your answer.
只要 name,回来的就只有那一列—— 年龄和成绩仍然在表里,只是不在你的结果里。
FROM names the table to take them from.
FROM 指明从哪张表里取。
And if you want everything, a star means every column.
而如果你想要全部,一个星号就表示所有列。
WHERE does the other cut.
WHERE 做的是另一个方向的切割。
It keeps only the rows whose condition is true — so asking for grade A, Ben drops out, and Ann and Cara stay.
它只保留条件为真的那些行—— 所以查成绩为 A 的时候,Ben 被筛掉,Ann 和 Cara 留下。
Put the two together and you can see the difference: SELECT cuts down the page, WHERE cuts across it.
把两者放在一起,区别就看得很清楚: SELECT 是竖着切,WHERE 是横着切。
One detail that catches everyone once: text in a condition goes in single quotes.
有一个细节每个人都会栽一次: 条件里的文本要写在单引号里。
The third task sorts the answer.
第三道题要给结果排序。
ORDER BY takes a column, and DESC means largest first — so Cara at seventeen at the top, then Ann, then Ben.
ORDER BY 接一个列名,DESC 表示从大到小—— 所以 17 岁的 Cara 排在最上面,然后是 Ann,然后是 Ben。
Leave DESC off and you get smallest first, which is the default.
不写 DESC 就是从小到大,那是默认值。
And one thing to memorise for the paper: the clauses always come in this order — SELECT, FROM, WHERE, ORDER BY.
还有一件要背下来的事: 这些子句的顺序永远是 SELECT、FROM、WHERE、ORDER BY。
Four things to take with you.
带走四点。
One: a row is a record and a column is a field.
第一:一行是一条记录,一列是一个字段。
Two: SELECT chooses the columns, and FROM names the table.
第二:SELECT 挑选列,FROM 指明表。
Three: WHERE keeps only the rows whose condition is true.
第三:WHERE 只保留条件为真的那些行。
Four: text in a condition goes in single quotes.
第四:条件里的文本写在单引号里。
Now write the three queries.
现在把那三条查询写出来。