A-level Computer Science revision · Exchanging data

Changing data with SQL and referential integrity

The idea

INSERT INTO adds records, UPDATE ... SET changes them and DELETE FROM removes them. Nested queries use one SELECT inside another. Referential integrity means a foreign key value must refer to an existing record (or be empty if allowed), so you cannot delete a record that others depend on without handling them.

  1. INSERT INTO table (fields) VALUES (...).
  2. UPDATE table SET field = value WHERE condition.
  3. DELETE FROM table WHERE condition.

Watch out UPDATE or DELETE without a WHERE clause changes every record in the table.

See it worked

Worked example

Write SQL to raise the price of product 17 to 4.99.

  1. UPDATE Product
  2. SET Price = 4.99
  3. WHERE ProductID = 17

Answer UPDATE Product SET Price = 4.99 WHERE ProductID = 17

Your turn

Question 11 mark

What does DELETE FROM Customer do?

Choose one
Question 21 mark

What is referential integrity?

Choose one
Question 32 marks

Write SQL to add a new student with ID 205, name 'Amy' and year 12.

Write your answer, then see a model answer
  1. INSERT INTO with fields.
  2. VALUES in the same order.

Answer INSERT INTO Student (StudentID, Name, Year) VALUES (205, 'Amy', 12)

A-level Computer Science Pro, OCR H446

A-level Computer Science Pro

£119 once

Price rises to £199 on 1 January. Save £80 by joining before then.

At least 12 months of access

By paying you agree to our Terms and get access straight away, which ends your 14-day cancellation right.

No VAT is charged.

Independent practice for OCR A-level Computer Science (H446), not endorsed by OCR.

Privacy · Terms