This topic covers databases: their structure and forms, calculations, sorting and searching, and reports.
18.1 Create a database structure
Database structure
A database table 数据表 holds records 记录 (rows), each made of fields (columns).
| data type | use | display formats |
|---|---|---|
| text (alphanumeric) | names, codes, phone numbers | |
| numeric: integer, decimal, currency | quantities, prices | number of decimal places, percentage, currency symbol |
| date/time | dates and times | e.g. 25/02/2026, 25-Feb-2026, 14:30 |
| Boolean 布尔型 (logical) | only two values | yes/no, true/false, a checkbox |
- data can be imported from .csv or .txt files, giving each field the specified name
Keys and relationships
- a primary key 主键 is a field whose value is unique for each record, e.g.
StudentID; it identifies the record - a foreign key 外键 is a field in one table that is the primary key of another table; it links the two tables
- a relationship 关系 joins the tables through these keys, e.g. one student has many loans
| flat-file database 平面文件数据库 | relational database 关系型数据库 |
|---|---|
| one table | two or more linked tables |
| simple to set up and use | more complex to design |
| data may be repeated (redundancy 冗余), wasting space | a good design reduces repetition: shared details stored once |
| repeated data can become inconsistent when changed in one place only | change once, correct everywhere |
| suits small, simple lists | suits large systems, e.g. a school or shop |
Good data entry forms
A clear title and instructions; fields in a logical order; enough white space and suitable spacing between fields; a clear, readable font; radio buttons 单选按钮 (choose one option), check boxes 复选框 (yes/no or several options), drop-down menus 下拉菜单 (choose from a list) to reduce typing errors; buttons to move between records.
Worked example. A library has Books(BookID, Title, Author) and Loans(LoanID, BookID, MemberID, DateOut). LoanID is the primary key of Loans. BookID is the primary key of Books and a foreign key in Loans, so each loan links to one book.
18.2 Manipulate data
Calculations in a database
- a calculated field 计算字段 in a query 查询 works out a new value for every record, e.g.
Total: [Price]*[Quantity] - a calculated control 计算控件 in a report 报表 or form shows a result, e.g.
=Sum([Total])in the report footer 报表页脚 - functions: Sum, Avg (average), Max, Min, Count; operators 运算符
+ − * /
Sorting
- ascending 升序 (A→Z, 0→9, oldest→newest) or descending 降序 (Z→A, 9→0)
- with multiple criteria 条件, a second sort orders records that tie on the first, e.g. by
Surnameascending, thenFirstNameascending
Searching with queries
A query selects only the records that meet the criteria. This sheet uses Microsoft Access syntax, with * and ? as wildcards 通配符; other software may use % and _.
| operator | meaning | example criteria |
|---|---|---|
= <> |
equal; not equal | Colour = "Red"; Colour <> "Red" |
> < >= <= |
greater, less (or equal) | Price >= 100 |
AND |
both conditions true | Price < 50 AND Stock > 0 |
OR |
either condition true | Colour = "Red" OR Colour = "Blue" |
NOT |
excludes | NOT "London" |
LIKE with wildcards |
pattern match: * any number of characters, ? one character |
LIKE "T*" (starts with T); LIKE "*son" (ends in son) |
Worked example. A car database. Find all red or blue cars costing under 20 000 USD, newest first:
(Colour = "Red" OR Colour = "Blue") AND Price < 20000, sorted by Year descending.
The brackets keep the OR together: without them, every red car would be selected whatever its price.
18.3 Present data
Database reports
A report presents selected data for printing or display.
| part | appears | typical contents |
|---|---|---|
| report header 报表页眉 | once, at the start | report title, logo, date |
| page header 页面页眉 | top of every page | column labels (field names) |
| detail | once per record | the data |
| page footer | bottom of every page | page number |
| report footer | once, at the end | totals, averages, counts |
Layout and display
- tabular 表格形式 layout: records in rows, fields in columns, like a table: good for many records
- columnar 纵栏形式 layout: one record at a time, labels beside the data: good for showing all the details of each record
- display all the required data and labels in full: widen controls so nothing is cut off
- right-align numbers and use decimal alignment 小数点对齐, so digits and decimal points line up; text is usually left-aligned
- format numbers: number of decimal places, currency symbol, percentage
Worked example. A sales report: title "Sales by Region" in the report header; field labels in the page header; each sale in the detail; page numbers in the page footer; the total of all sales, formatted as currency with 2 decimal places, in the report footer.