Databases and SELECT · 数据库与 SELECT
Databases and tables
A database stores information in tables. A table is a grid, a bit like a spreadsheet:
- each row is one record — for example, one student;
- each column is one field — for example, the student's name or score.
This course uses a small table called student. It has 5 records and 4 fields: id, name, form, and score.
数据库与表
数据库把信息存放在表里。表是一个网格,有点像电子表格:
- 每一行是一条记录 —— 例如,一名学生;
- 每一列是一个字段 —— 例如,学生的姓名或分数。
本课程使用一张小表,叫 student。它有 5 条记录、4 个字段:id、name、form 和 score。
Asking for data with SELECT
To read data from a table you write a query with SELECT.
The simplest query asks for every column of every row. The star * means "all columns":
SELECTchooses the columns —*means all of them.FROM studentsays which table to read.
The database replies with a result table (also called a result set): the rows your query asked for.
用 SELECT 取数据
要从表里读取数据,你用 SELECT 写一条查询。
最简单的查询取每一行的每一列。星号 * 表示“所有列”:
SELECT * FROM student;
SELECT选择列 ——*表示全部列。FROM student指定读取哪张表。
数据库会返回一张结果表(也叫结果集):就是你的查询要的那些行。
A few rules
- Keywords like
SELECTandFROMare often written in capitals so they stand out. - End each statement with a semicolon
;. - An empty cell is shown as
NULL, which means "no value".
Now try it. Click Run to see the output, then Check answer.
几条规则
- 像
SELECT和FROM这样的关键字常写成大写,这样更醒目。 - 每条语句以分号
;结尾。 - 空单元格显示为
NULL,意思是“没有值”。
现在试一试。点击运行查看输出,然后点击检查答案。
Common mistakes
- End a query with a semicolon:
SELECT ... FROM table;. SELECT *returns every column — name only the ones you need.
常见错误
- 查询以分号结尾:
SELECT ... FROM table;。 SELECT *返回所有列——只选你需要的。
SELECT … FROM … WHERE
WHERE keeps rows, then SELECT keeps columns — step through it. · WHERE 保留行,然后 SELECT 保留列——逐步查看。
Show the whole student table — every column and every row. Use SELECT * FROM ...;. · 显示整张 student 表 —— 所有列、所有行。使用 SELECT * FROM ...;。
Click Run to see the output here. · 点击“运行”查看此处输出。
Now keep only some columns and some rows · 行. Show just the name and score of students in form 11A. Pick the two columns and add a WHERE form = '11A'. · 现在只保留部分列和部分行。只显示 11A 班学生的 name 和 score。选出这两列,并加上 WHERE form = '11A'。
Click Run to see the output here. · 点击“运行”查看此处输出。