- Understand how to add, change, and remove records in a database using SQL.
- Understand how to test database changes by running SELECT queries before and after.
- Understand how to evaluate a database solution against the three standard criteria.
- I can write an
INSERT INTO … VALUESstatement to add a new row to a table. - I can write an
UPDATE … SET … WHEREstatement to change values in existing rows. - I can write a
DELETE FROM … WHEREstatement to remove a row safely. - I can use
SELECTbefore and after a change to verify the database is correct. - I can evaluate a database using the two DDD criteria: fitness for purpose and accuracy of output.
Use the same films database from previous SQL lessons. If you need a fresh copy, download and re-run the file in DataGrip.
Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.
1. Which SQL command is used to add a completely new row to a table?
2. A pupil wants to change the rating of film 1 to 8.1. Which SQL command should they use?
3. Which SQL command removes rows from a table?
Key vocabulary
SQL: Updating Data, Testing, and Evaluation
INSERT — adding new rows
The INSERT INTO command adds a new row to a table. You list the table name followed by VALUES and then the data for each column in order, separated by commas. Text values are wrapped in quote marks; numbers are not.
The values must match the column order defined in the table. For the director table, that order is director_id, first_name, last_name, nationality. If the table has a foreign key, you must insert the parent row before the child row — in the films database, you must always insert the director before you insert the film that references them.
UPDATE — changing existing rows
The UPDATE command changes data that is already stored in the table. You name the table, use SET to specify which column to change and its new value, and use WHERE to specify which row(s) to change.
SET rating = 8.5
WHERE film_id = 53;
The WHERE clause is critical. If you leave it out, every row in the table is updated. For example, UPDATE film SET rating = 5.0; with no WHERE would change the rating of all 52 films to 5.0. Always double-check your WHERE condition before running an UPDATE.
You can update more than one column at once by separating them with commas inside the SET clause:
SET certificate = '15', runtime_mins = 146
WHERE film_id = 53;
DELETE — removing rows
The DELETE FROM command removes rows from a table. Like UPDATE, it uses WHERE to target specific rows. Without WHERE, all rows in the table are deleted.
When a foreign key is involved, you must delete the child row first (the row that contains the foreign key) before you delete the parent row. In the films database, film is the child table — it references director. If you try to delete a director who still has films, MySQL will refuse with a foreign key constraint error. The safe order is: delete the film, then delete the director.
Testing changes with SELECT
Whenever you run an INSERT, UPDATE, or DELETE, you should test that the change worked correctly. The standard approach is:
- Before: run a
SELECTto see the current state of the relevant rows. - Run your INSERT / UPDATE / DELETE.
- After: run the same
SELECTagain and check the result matches what you expected.
For an INSERT, you expect the SELECT to return the new row. For an UPDATE, you expect the changed column to show the new value. For a DELETE, you expect the SELECT to return zero rows. This before-and-after approach is a key part of database testing and is directly examined in the N5 question paper.
Referential integrity in practice
Before deleting a parent row (such as a director), it is good practice to run a SELECT first to check whether any child rows still reference it. This prevents the foreign key constraint error.
SELECT * FROM film WHERE director_id = 16;
If the SELECT returns rows, delete those films first. If it returns zero rows, it is safe to delete the director.
Evaluation — the two DDD criteria
After building or testing a database, you may be asked to evaluate it. The N5 specification (Appendix 8) requires you to evaluate a database using exactly two criteria:
- Fitness for purpose: Does the database store the right data and allow the end user to carry out the tasks they need? To answer this, describe what the user's requirements are and explain whether the database meets them. For example: "The films database stores title, year, rating, genre, and director, which allows users to search and filter the film collection as required — it is fit for purpose."
- Accuracy of output: When a query is run, does it return the correct records, in the correct fields, matching what was expected? To answer this, describe the expected output, run the query, and compare. For example: "The query was expected to return all films rated above 8.0. It returned 4 rows — all with rating > 8.0 and no other rows included — so the output is accurate."
Note: Robustness and efficient use of constructs are SDD (software) criteria — they do not appear in the DDD evaluation spec. Do not use them in a database evaluation question.
In the exam, always name the criterion, then explain it in the context of the specific database described in the question. A vague answer like "it works well" scores no marks.
Worked examples
Task: Add director Stanley Kubrick (director_id 16, American) and his film The Shining (film_id 53, 1980, certificate '18', 146 minutes, rating 8.4, Horror) to the films database.
director table is the parent. The foreign key in film references it, so the director must exist before the film is added.film_id, title, year, certificate, runtime_mins, rating, genre, director_id.INSERT INTO director VALUES (16, 'Stanley', 'Kubrick', 'American');
-- Step 2: insert the film
INSERT INTO film VALUES (53, 'The Shining', 1980, '18', 146, 8.4, 'Horror', 16);
-- Step 3: verify
SELECT * FROM film WHERE film_id = 53;
Verified result: the SELECT returns 1 row — The Shining, 1980, '18', 146, 8.4, Horror, director_id 16.
Task: A reviewer notes that The Shining should have both the certificate updated to '15' and the runtime corrected to 144 minutes. Update both columns in a single statement and verify.
SELECT film_id, title, certificate, runtime_mins FROM film WHERE film_id = 53;
-- Update two columns at once
UPDATE film
SET certificate = '15', runtime_mins = 144
WHERE film_id = 53;
-- After snapshot
SELECT film_id, title, certificate, runtime_mins FROM film WHERE film_id = 53;
Verified result: the after SELECT shows The Shining with certificate '15' and runtime_mins 144. The before SELECT had '18' and 146.
Task: Remove The Shining and Kubrick from the database safely. There is a foreign key linking the film to the director, so the order of deletions matters.
SELECT * FROM film WHERE director_id = 16;
-- Returns 1 row, so delete that film first
-- Step 2: delete child row, then parent row
DELETE FROM film WHERE film_id = 53;
DELETE FROM director WHERE director_id = 16;
-- Step 3: verify both gone
SELECT * FROM film WHERE film_id = 53;
SELECT * FROM director WHERE director_id = 16;
Verified result: both SELECT queries return 0 rows. If you delete the director before the film, MySQL returns: Cannot delete or update a parent row: a foreign key constraint fails.
Task: Write a short evaluation of the films database covering the two N5 DDD criteria.
A new director needs to be added to the films database: Hayao Miyazaki, director_id 17, Japanese. His film Spirited Away also needs to be added: film_id 54, year 2001, certificate 'PG', runtime 125 minutes, rating 8.6, genre 'Animation'. After adding both, a reviewer asks you to update the rating to 8.7.
Answer the following:
- Write the INSERT statement to add Miyazaki to the
directortable. - Write the INSERT statement to add Spirited Away to the
filmtable. - Write the UPDATE statement to change the film's rating to 8.7. Include the WHERE clause.
-
INSERT INTO director VALUES (17, 'Hayao', 'Miyazaki', 'Japanese');
-
INSERT INTO film VALUES (54, 'Spirited Away', 2001, 'PG', 125, 8.6, 'Animation', 17);
-
UPDATE film
SET rating = 8.7
WHERE film_id = 54;The WHERE clause must target the specific film. Without it, every film's rating would be set to 8.7.
'Horror'. Numbers (INT, DECIMAL) must not have quotes: 8.4, not '8.4'. Putting quotes around a number, or removing quotes from text, causes a MySQL error.DDD evaluation questions ask about exactly two criteria: fitness for purpose and accuracy of output. Do not write about robustness or efficient use of constructs — those are SDD criteria and will score no marks in a DDD question. For each criterion use this structure: (1) state the criterion by name, (2) explain what it means, (3) apply it to the specific database in the question. For example: "The query output is accurate because it returned all films with rating > 8.0 — these are the expected records and no incorrect rows were included." Name, meaning, application. A vague one-sentence answer rarely scores full marks.
Questions 1–5 are auto-checked. Questions 6–7 are self-marked — write your answer, then reveal the model answer to check your work. Questions 8–9 are DataGrip practical tasks.
1. Which SQL command adds a new row to a table? TYPE 1
2. A pupil writes UPDATE film SET rating = 9.5; with no WHERE clause. Which rows are affected? TYPE 1
3. The film table has a foreign key referencing director. You want to delete director_id 5 and all their films. What is the correct order? TYPE 1
4. An evaluator says: "The database stores the correct fields and allows users to search for films by genre and rating, which is what they need." Which evaluation criterion does this describe? TYPE 1
5. Which SQL statement correctly deletes only the film with film_id = 7? TYPE 1
6. A pupil runs the following query: SELECT title, rating FROM film WHERE genre = 'Drama'; Describe how this query should be tested. TYPE 2
Write 3–4 sentences. Do not refer to normal/extreme/exceptional data — those are software testing categories, not database testing.
Before running the query, identify the expected output: the result should show the title and rating columns for every film where genre is 'Drama', and no other films.
Run the query in DataGrip and examine the results. Check that every row returned has genre = 'Drama'. Then run SELECT COUNT(*) FROM film WHERE genre = 'Drama'; to confirm the number of rows matches.
If the actual output matches the expected output — correct fields, correct rows, no missing or extra records — the query output is accurate. If any Drama films are missing, or non-Drama films appear, the query has an error that needs correcting.
7. Write a short evaluation of the films database. Use both DDD evaluation criteria: fitness for purpose and accuracy of output. Write 1–2 sentences for each. TYPE 2
Fitness for purpose: The films database is fit for purpose because it stores all the information the end user requires — title, year, certificate, rating, genre, and director — and allows users to search and filter the collection using SQL queries. This meets the stated functional requirements.
Accuracy of output: A query was run to find all films directed by director_id 1. The expected output was a list of that director's films showing title and year. The query returned the correct rows with no extra or missing records, so the output is accurate.
8. In DataGrip, run the SQL below to add a test director and film, then verify the insert and update the rating. Record the result of each SELECT. TYPE 3
Practical Task 1 of 2 — INSERT and UPDATE test records
Open a query console for the films database in DataGrip. Run these statements one block at a time:
INSERT INTO director VALUES (99, 'Test', 'Director', 'Test');
INSERT INTO film VALUES (99, 'Test Film', 2024, 'U', 90, 7.5, 'Drama', 99);
-- Verify the insert
SELECT * FROM film WHERE film_id = 99;
-- Update the rating and verify
UPDATE film SET rating = 8.0 WHERE film_id = 99;
SELECT film_id, title, rating FROM film WHERE film_id = 99;
- Paste the result of the first SELECT (should show Test Film with rating 7.5).
- Paste the result of the second SELECT (should show rating 8.0 after the UPDATE).
First SELECT result (after INSERT): 1 row — film_id 99, 'Test Film', 2024, 'U', 90, 7.5, 'Drama', director_id 99.
Second SELECT result (after UPDATE): 1 row — film_id 99, 'Test Film', rating 8.0. The rating changed from 7.5 to 8.0, confirming the UPDATE worked correctly.
9. In DataGrip, delete your test film and director from Q8, then verify both are gone. Record each SELECT result. TYPE 3
Practical Task 2 of 2 — DELETE and verify
Still in the same query console. Remember: delete the child row (film) before the parent row (director). Then verify each deletion.
DELETE FROM film WHERE film_id = 99;
DELETE FROM director WHERE director_id = 99;
-- Verify both are gone
SELECT * FROM film WHERE film_id = 99;
SELECT * FROM director WHERE director_id = 99;
- Paste the film SELECT result (should return 0 rows).
- Paste the director SELECT result (should return 0 rows).
- Explain in one sentence why the film had to be deleted before the director.
Film SELECT result: 0 rows returned — the film has been successfully deleted.
Director SELECT result: 0 rows returned — the director has been successfully deleted.
Why film first: The film table has a foreign key referencing director. MySQL enforces referential integrity, so it will not allow you to delete a director row that is still referenced by a film row. Deleting the film first removes the reference, making it safe to delete the director.
Suggested timing: 60 minutes. Warm up 8 min; notes + examples 20 min; Now you try 5 min; task set 25 min (Q1–5 auto, Q6–7 written, Q8–9 DataGrip); wrap-up 2 min.
Key misconceptions to address:
- Pupils frequently omit WHERE from UPDATE or DELETE — run a live demo of
UPDATE film SET rating = 1.0;(with Laragon in demo mode or on a test database) to show all rows affected. Roll back or reinstate afterwards. - Pupils confuse DELETE (removes rows) with DROP (removes an entire table). Stress the difference explicitly.
- Evaluation answers tend to be too vague. Model the two-part structure: (1) name the criterion, (2) apply it to the specific database — "The output is accurate because…". Stress that there are exactly TWO DDD criteria: fitness for purpose and accuracy of output. Robustness and efficient use of constructs are SDD criteria — do not let pupils write these in a DDD question.
Live demo suggestion: Work through the Kubrick/Shining INSERT example (film_id 53, director_id 16) in DataGrip with the class watching. After inserting, attempt to delete the director before the film to show the FK constraint error message live. Then delete in the correct order.
Note on worked example data: The worked examples add director_id 16 (Kubrick) and film_id 53 (The Shining) to the films database. These can be left in as valid additions, or pupils can delete them after the example. The Now you try adds director_id 17 (Miyazaki) and film_id 54 (Spirited Away) — again, valid data that can remain. Practical tasks Q8–Q9 use IDs 99 specifically because they are clearly test records intended to be deleted at the end.
Extension question: Ask pupils to write a query that would update the runtime for all films where runtime_mins is NULL. This requires identifying which rows have NULL (WHERE runtime_mins IS NULL) and choosing a sensible value — there is no single correct answer, which makes it a good discussion point.
SQA command words covered: "describe", "explain", "evaluate", "identify".