OCR focus
OCR: open, read, write and close
A text file retains an ordered collection of lines between program runs. A working variable holds temporary text in the current run. Opening an existing file provides a handle for file operations; closing releases that handle without deleting the stored lines. A new file must be created before it is opened.
In OCR Exam Reference Language, writeLine adds a line at the END of the file. It does not replace a named person's earlier record. readLine copies the next line into working state without deleting it, then advances the read position. endOfFile is a Boolean check: True means no unread line remains. Do not attempt another read after that result.
Trace file contents, working variables and the next read position separately. A clean new run starts with fresh temporary state while the file remains. If a question explicitly describes a different overwriting operation, follow that stated behaviour; do not assume OCR writeLine overwrites or that OCR open requires a Python-style mode argument.
Worked example: persist two scores
- The file initially contains exactly one text line: Mina,12. The comma and digits are stored text; line numbers are only a reading guide.
- Prepare Leo,15 in a working variable. Open the existing file and use writeLine to append it after Mina,12. Both stored lines remain in that order.
- Close the handle, start a clean new run and reopen. The first read gives Mina,12; the second replaces the working text with Leo,15. Neither read deletes a stored line.
- After both reads, endOfFile is True. There is no third line. Change only the working variable to Leo,99 without writing; the saved second line remains Leo,15.
- Close, restart and reopen again: the stored lines are still Mina,12 and Leo,15. In a separate reset comparison, an operation explicitly defined to clear the original file before writing Leo,15 would leave only that one line.
Appending, reading and assigning a working variable change different state. Only an appropriate file-writing operation changes the stored contents; closing does not clear an independently held variable.
AQA focus
AQA: relationships, primary keys and foreign keys
A database is an organised collection of data. A relational database stores related data in tables. A record is one row; a field is one named attribute or column, with a suitable data type. In this example, MemberID is an integer, while Name and Town contain text.
A primary key uniquely identifies a record in its table. A foreign key refers to a primary key in another table, linking the records. Two members can share the name Mina: MemberID 18 identifies the Mina in York, while MemberID 20 identifies a different Mina in Hull. Match identifiers, not names or row positions.
MemberID 18 can appear in several Loan records because one member can borrow several books. This useful repeated reference is different from copying her town into every loan. Storing the town once reduces redundancy and the risk of inconsistent copies when it changes. A database does not automatically repair a poorly chosen design.
| Table | Example record | Key and relationship |
|---|---|---|
| Member | MemberID 18; Name Mina; Town York | MemberID is the primary key |
| Member | MemberID 19; Name Leo; Town Bath | A different member |
| Member | MemberID 20; Name Mina; Town Hull | Same name as member 18; different primary-key value |
| Loan | LoanID 51; MemberID 18; BookName Orbit | LoanID is the primary key; MemberID is a foreign key |
| Loan | LoanID 52; MemberID 18; BookName Tides | Both loans link to member 18 |
| Loan | LoanID 53; MemberID 19; BookName Spark | This loan belongs to Leo |
Worked example: member 18 moves from York to Leeds
- Change Town in member 18's record to Leeds. Keep her MemberID as 18.
- Leave both Loan records unchanged: their MemberID values still refer to 18.
- Look up either loan's borrower again. Both now recover Leeds from the same Member record.
One Member record changes; no Loan record changes. A lookup reads the relationship rather than copying a new town into each loan.
Select columns and filter rows
SELECT names the fields returned; FROM identifies the source table; WHERE tests which records qualify. A condition can test Score without returning Score. Filtering excludes rows from the result, not from the stored table. Write the clauses in SELECT, FROM, WHERE order; the reasoning trace is not a claim about a database engine's execution plan.
Check the exact boundary: >= 50 includes fifty, while > 50 excludes it. AND requires both conditions to be true for the same record; OR requires one or both. Write a text value such as 'North' in single quotes, while a numeric threshold such as 50 is unquoted.
SELECT Name, Score returns two fields; SELECT * returns every field. Without WHERE, every record qualifies. A condition matching no records produces zero result rows, not a blank record. Without ORDER BY the query does not guarantee result order; supplied-table order is used here only to present an inspectable trace.
| EntryID | Name | Score | Team |
|---|---|---|---|
| 11 | Mina | 72 | North |
| 12 | Leo | 50 | South |
| 13 | Sam | 49 | North |
| 14 | Iris | 88 | South |
| 15 | Ruth | 60 | North |
Worked example: SELECT Name FROM Result WHERE Score >= 50
- Test each record's score: 72, 50, 88 and 60 qualify; 49 does not.
- Return only Name for those records: Mina, Leo, Iris and Ruth. Keep all five source records unchanged.
- Changing SELECT Name to SELECT Score returns their scores instead. Changing >= to > excludes Leo.
- Add AND Team = 'North': only Mina and Ruth qualify. Change AND to OR: all five qualify, because Sam meets the team test and Leo/Iris meet the score test.
The condition chooses records; SELECT chooses returned fields. A two-field result has two columns, independently of how many records qualify.
AQA focus
AQA: ordering and querying two linked tables
Use this separate library example with a new set of member and loan records. Member.MemberID names the MemberID field inside Member; Loan.MemberID names the borrower reference inside Loan. LoanID identifies a loan, not its borrower. AQA queries can retrieve data from up to two tables.
The equality Member.MemberID = Loan.MemberID links the right records. The separate condition Member.MemberID = 11 selects Mina. Both conditions must hold. Listing Member and Loan in FROM does not supply the missing equality.
ORDER BY arranges complete result records: ASC is ascending and DESC is descending. For these capitalised titles, ascending is alphabetic order. WHERE filters records; ORDER BY arranges those that qualify. Neither operation changes the stored tables. Without ORDER BY, do not assume a particular result order. Equal titles remain separate records; this ordering key alone does not specify their relative order.
| Table | Record | Meaning |
|---|---|---|
| Member | MemberID 11; Name Mina | Requested member |
| Member | MemberID 12; Name Leo | A different member |
| Member | MemberID 13; Name Ruth | A different member |
| Loan | LoanID 71; MemberID 12; BookTitle Tides | Leo's loan |
| Loan | LoanID 72; MemberID 11; BookTitle Orbit | Mina's loan |
| Loan | LoanID 73; MemberID 11; BookTitle Atlas | Mina's loan |
| Loan | LoanID 74; MemberID 13; BookTitle Reef | Ruth's loan |
| Loan | LoanID 75; MemberID 11; BookTitle Tides | Mina's loan |
Worked example: return Mina's books in title order
- Use SELECT Member.Name, Loan.BookTitle FROM Member, Loan.
- Use WHERE Member.MemberID = Loan.MemberID AND Member.MemberID = 11. Loans 72, 73 and 75 qualify; loans 71 and 74 do not.
- Use ORDER BY Loan.BookTitle ASC. Return Mina with Atlas, then Mina with Orbit, then Mina with Tides. Her name appears once per matching loan.
- Change only ASC to DESC to return Tides, Orbit, Atlas. All three Member records and all five Loan records remain stored.
Without the key equality, the member filter alone would pair Mina with all five loans. Correct the relationship rather than deleting another person's stored loans.
Continue with your exam board
The course combines lesson videos, interactive activities, topic questions and original exam mocks. Sign in to use the learning tools and save your progress; your account’s course access applies.
Independent learning resources. Not endorsed by AQA or OCR. These public introductions complement the full course and the official specification.