Exam-style questions across all four lessons. Work through them in order, then reveal mark-scheme style answers using the button on each question.
State the difference between a flat-file database and a relational database.
2 marksA flat-file database stores all data in a single table [1]; a relational database splits data across multiple linked tables [1].
Accept: flat-file causes redundancy; relational uses primary/foreign keys to link tables.
The table below is from a flat-file database used by a school.
| StudentID | StudentName | CourseName | CourseCode | TeacherName |
|---|---|---|---|---|
| 1 | Alice Smith | GCSE Maths | MA11 | Mr Jones |
| 2 | Bob Brown | GCSE Maths | MA11 | Mr Jones |
| 3 | Clara Davis | GCSE CS | CS11 | Ms Patel |
(b) TeacherName appears in multiple rows [1]; if only some rows are updated, the data becomes inconsistent/contradictory [1]. (Update anomaly - accept any valid description)
(c) In a relational database, teacher information would be stored in a separate Teachers table [1]; each course would reference the teacher using a foreign key [1]; the teacher's name is stored only once so a single update keeps all data consistent [1].
Give three rules that a primary key must satisfy.
3 marksIt must be unique (no two records can share the same value) [1]; it must not be null/empty [1]; it should be stable (its value should not change) [1]; it must identify exactly one record in the table [1].
A database stores information about employees and their departments. Suggest an appropriate data type for each of the following fields, giving a reason for each choice.
(b) Boolean [1]; because the field can only hold one of two values: TRUE or FALSE / yes or no [1].
(c) Real/Float [1]; because the hourly rate may include pence (decimal portion) - an integer would round down and lose precision [1].
Explain the purpose of a foreign key in a relational database.
2 marksAccept: allows data to be retrieved from multiple tables; maintains referential integrity.
A music streaming service has two entities: Users (UserID, Username, Email) and Playlists (PlaylistID, PlaylistName, UserID).
(b) One-to-many (one user has many playlists) [1].
(c) A junction/link table is needed, e.g. UserLikes [1]; containing UserID (FK) and SongID (FK) [1]; the primary key could be a composite of UserID + SongID [1]. Accept any valid junction table with both foreign keys correctly identified.
Use the following table for questions 7 to 10:
| StudentID | FirstName | LastName | Age | Year | IsEnrolled | Score |
|---|---|---|---|---|---|---|
| 1001 | Alice | Smith | 15 | 10 | TRUE | 82 |
| 1002 | Bob | Brown | 16 | 11 | TRUE | 67 |
| 1003 | Clara | Davis | 15 | 10 | FALSE | 91 |
| 1004 | Dan | Evans | 16 | 11 | TRUE | 54 |
| 1005 | Eve | Foster | 15 | 10 | TRUE | 78 |
| 1006 | Frank | Green | 17 | 12 | TRUE | 43 |
| 1007 | Grace | Hill | 16 | 11 | FALSE | 88 |
Write a SQL query to retrieve the first name and score of all students in Year 10.
2 marksFROM Students
WHERE Year = 10
Write a SQL query to retrieve the full name (both fields) of all enrolled students with a score greater than 70, ordered by score from highest to lowest.
4 marksFROM Students
WHERE IsEnrolled = TRUE AND Score > 70
ORDER BY Score DESC
State the names returned by the following query:
Write a SQL query to add a new student to the table: ID 1008, Jade King, age 15, Year 10, enrolled, score 72.
3 marksVALUES (1008, 'Jade', 'King', 15, 10, TRUE, 72)
Frank Green has left school. Write a SQL statement to remove his record from the Students table. State one danger of this type of command.
3 marksExplain what a SQL injection attack is and describe how it could be used to bypass a website's login system.
4 marks' OR '1'='1 makes the WHERE condition always true, returning all rows and bypassing the password check [1].Max 4 marks. Award marks for: definition, mechanism, effect/result, specific example.
Explain how parameterised queries prevent SQL injection attacks.
3 marksA web application builds a SQL query using this vulnerable code:
admin'--. Write the full SQL query that would be executed. [2](b) The -- is a SQL comment character [1]; everything after it (including the closing quote and any password check) is ignored by the database, so the query returns the admin account without checking a password [1].
(c)
cursor.execute(query, (username,))