TopMyGrade

GCSE/Computer Science/AQA

CS7.3SQL — INSERT, UPDATE, DELETE: adding new rows, changing existing rows and removing rows; effect on referential integrity

Notes

SQL — modifying data with INSERT, UPDATE, DELETE

SELECT only reads data. To change the contents of a database, use INSERT, UPDATE and DELETE. These are sometimes called DML (Data Manipulation Language) statements.

INSERT — add a new row

INSERT INTO Students (StudentID, Name, DOB, TutorGroup)
VALUES (1004, 'Daniel', '2009-04-08', '10C');

You list the columns and the corresponding values. The order matters — values match the column list.

If you provide values for every column in table order, you can omit the column list:

INSERT INTO Students VALUES (1004, 'Daniel', '2009-04-08', '10C');

But it's safer to list the columns explicitly — it survives column reordering.

Multi-row insert (extension)

INSERT INTO Subjects (SubjectID, Name) VALUES
  ('S04', 'Geography'),
  ('S05', 'History');

UPDATE — change existing rows

UPDATE Students
SET TutorGroup = '11A'
WHERE StudentID = 1004;
  • SET — which columns to change and to what value.
  • WHERE — which rows are affected.

You can change multiple columns at once:

UPDATE Students
SET TutorGroup = '11A', YearGroup = 11
WHERE TutorGroup = '10A';

Critical: forgetting the WHERE clause updates every row in the table. Test with a SELECT first.

DELETE — remove rows

DELETE FROM Students
WHERE StudentID = 1004;
  • DELETE FROM table — choose the table.
  • WHERE — which rows to remove.

Like UPDATE, missing WHERE deletes everything in the table:

DELETE FROM Students;   -- removes ALL rows. Be very careful.

To delete the entire table including its structure, use DROP TABLE Students; (DDL, not DML).

Effect on referential integrity

Suppose Enrolments has StudentID as a foreign key to Students. If you try:

DELETE FROM Students WHERE StudentID = 1001;

The DBMS may refuse if 1001 has rows in Enrolments — this would leave orphan foreign keys. Behaviour depends on rule:

  • RESTRICT (default) — refuse to delete.
  • CASCADE — also delete the dependent rows in Enrolments.
  • SET NULL — set the foreign key to NULL.

Choose the rule when defining the relationship, not at delete time.

Worked exampleWorked example — manage a library

-- Add a new book
INSERT INTO Books (BookID, Title, Author, Year)
VALUES (101, 'Project Hail Mary', 'Andy Weir', 2021);

-- Mark the book as on loan
UPDATE Books
SET Available = 0
WHERE BookID = 101;

-- Remove a deleted/destroyed book
DELETE FROM Books
WHERE BookID = 5;

Best practices

  • Always include WHERE in UPDATE and DELETE — even when "obvious".
  • Test the WHERE first with SELECT — make sure it picks the rows you intend.
  • Use transactions to wrap related changes (BEGIN / COMMIT / ROLLBACK).
  • Backups before any large change.
  • Audit logs for who changed what.

Common pattern — soft delete

Instead of DELETE, set a deleted = TRUE flag. Lets you recover and preserves audit trail.

UPDATE Students SET Deleted = 1 WHERE StudentID = 1004;

Common mistakesPitfalls

  1. UPDATE without WHERE — updates every row.
  2. DELETE without WHERE — deletes every row.
  3. Mismatched columns/values in INSERT. Order and count must match.
  4. Inserting values that violate constraints (e.g. duplicate primary key) — INSERT fails.
  5. Forgetting referential integrity. A DELETE may be refused or cascade unexpectedly.

Worked exampleWorked example — orphan prevention

If a student has enrolments and you try DELETE FROM Students WHERE StudentID = 1001;:

  • With RESTRICT, the DBMS refuses — fix by deleting their enrolments first.
  • With CASCADE, both go in one operation — but check this is what you really want.
  • Without referential integrity rules, you get orphan rows in Enrolments — broken database.

Try thisQuick check

State the result of each, given Students has rows 1001-1003:

INSERT INTO Students (StudentID, Name) VALUES (1004, 'Dan');

→ Adds Dan as 1004.

UPDATE Students SET Name = 'Daniel' WHERE StudentID = 1004;

→ Renames Dan to Daniel.

DELETE FROM Students WHERE Name = 'Daniel';

→ Removes Daniel's row, leaving 1001-1003.

AI-generated · claude-opus-4-7 · v3-deep-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 14 marks

    INSERT row

    Write SQL to add a new row to a table Books(BookID, Title, Author, Year) with values 5, 'Dune', 'Herbert', 1965.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  2. Question 24 marks

    UPDATE row

    Write SQL to change the TutorGroup of student 1002 to '11B' in the Students table.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  3. Question 33 marks

    DELETE row

    Write SQL to delete the row in Books where BookID is 7.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  4. Question 44 marks

    UPDATE multiple columns

    Write SQL that increases the price by 10 and sets Discount to 0 for all products in category 'Books'.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  5. Question 54 marks

    Missing WHERE risk

    Explain what would happen if a programmer ran UPDATE Students SET TutorGroup = '11A'; and how to avoid this risk.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  6. Question 64 marks

    Referential integrity on DELETE

    A student row has dependent enrolments. Explain what happens (a) under RESTRICT and (b) under CASCADE when you DELETE the student.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  7. Question 72 marks

    INSERT failures

    Give two reasons an INSERT statement might fail.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

Flashcards

CS7.3 — SQL — INSERT, UPDATE and DELETE

12-card SR deck for AQA GCSE Computer Science topic CS7.3

12 cards · spaced repetition (SM-2)