A-Level Computer Science / Unit 8: Relational Databases and SQL

8.3.4 Inserting, Updating and Deleting Data

πŸ”’ Lesson slides are available to signed-in users. Sign in

8.3.4 Inserting, Updating and Deleting Data

A database must change as real-world information changes. SQL data-maintenance statements can add a new tuple, alter values in existing tuples or remove tuples that are no longer required. These operations must be written carefully because they change stored data rather than merely displaying a result.

By the end of this section, you should be able to:

  • Write an INSERT INTO statement that supplies values for named attributes.
  • Match each inserted value to a suitable attribute and data type.
  • Explain how primary-key, foreign-key and data-type constraints can reject an insertion.
  • Write an UPDATE ... SET ... WHERE statement to change selected tuples.
  • Predict the effect of omitting or widening an update condition.
  • Write a DELETE FROM ... WHERE statement to remove selected tuples.
  • Explain why a missing WHERE clause can affect every tuple.
  • Use referential integrity to determine a safe order for inserting or deleting related data.
  • Distinguish deleting tuples from deleting a table structure.
  • Trace the before-and-after state of a table after a maintenance statement.
Scope note: These are Data Manipulation Language operations. Database structures were created in 8.3.1, and query construction was covered in 8.3.2 and 8.3.3.

The EcoTrack Tables

The examples continue the original EcoTrack database. A research site is the parent table, and each sensor stores a foreign key that identifies its site.

ResearchSite

ResearchSite(SiteID, SiteName, RegionCode, Active)

Primary key: SiteID

Sensor

Sensor(SensorID, SiteID, SensorType, InstalledOn, DailyCheckTime, SampleInterval)

Primary key: SensorID Foreign key: SiteID

Current ResearchSite data

SiteID SiteName RegionCode Active
RS101Forest EdgeNWTRUE
RS205Estuary PointSETRUE
RS318Hill ReserveNWTRUE
RS424Wetland SouthSWFALSE

Current Sensor data

SensorID SiteID SensorType InstalledOn DailyCheckTime SampleInterval
S-104RS101Air quality2026-02-1407:3010
S-218RS205Soil moisture2025-09-0306:4530
S-311RS101Air quality2026-01-2708:0015
S-405RS424Water level2024-11-1905:505
S-522RS318Temperature2025-06-0809:1520
S-607RS318Air quality2024-04-2207:1060
S-714RS205Water level2026-03-1206:2010

Adding a Tuple with INSERT INTO

A clear insertion names the target table, lists the attributes that will receive values, and then supplies those values in the same order.

INSERT INTO Sensor
    (SensorID, SiteID, SensorType, InstalledOn,
     DailyCheckTime, SampleInterval)
VALUES
    ('S-826', 'RS318', 'Wind speed', '2026-05-18',
     '08:30', 12);
INSERT INTO Which table receives the tuple?

Sensor

Attribute list Which attributes are being supplied?

The six named Sensor attributes

VALUES What values enter those attributes?

Six values in the matching order

Some SQL systems permit an insertion without an attribute list when every value is provided in the table's defined order. Naming the attributes is clearer and avoids relying on remembered column order.

Matching values to attributes

Attribute Inserted value Reasonable representation
SensorID'S-826'Text identifier in quotation marks
SiteID'RS318'Text foreign-key value
SensorType'Wind speed'Text
InstalledOn'2026-05-18'Date value
DailyCheckTime'08:30'Time value
SampleInterval12Integer without quotation marks
Exam tip: Count the attribute names and values. The numbers must match, and the first value must belong to the first named attribute, the second value to the second attribute, and so on.

How the DBMS Checks an Insertion

An INSERT INTO statement is not accepted automatically. The DBMS checks the proposed tuple against the table definition and its constraints.

Check Example problem Likely result
Primary-key uniqueness SensorID = 'S-104' already exists. The tuple is rejected.
Referential integrity SiteID = 'RS999' has no parent ResearchSite tuple. The tuple is rejected.
Data type SampleInterval = 'frequent' is not an integer. The value is rejected.
Attribute/value count Six attributes are named but only five values are supplied. The statement is invalid.

Insert a parent before its child

Suppose a sensor will be installed at a completely new site. The parent ResearchSite tuple must exist before the child Sensor tuple can reference it.

INSERT INTO ResearchSite
    (SiteID, SiteName, RegionCode, Active)
VALUES
    ('RS530', 'Coastal Dune', 'NE', TRUE);

INSERT INTO Sensor
    (SensorID, SiteID, SensorType, InstalledOn,
     DailyCheckTime, SampleInterval)
VALUES
    ('S-913', 'RS530', 'Sand movement', '2026-06-02',
     '09:00', 20);
Common mistake: Inserting the child tuple first can violate referential integrity because its foreign-key value has nothing to reference yet.

Changing Existing Values with UPDATE

UPDATE identifies the table, SET states the new value, and WHERE selects the tuple or tuples to be changed.

UPDATE Sensor
SET SampleInterval = 8
WHERE SensorID = 'S-405';
1

UPDATE

Choose the target table.

2

SET

Define the replacement value.

3

WHERE

Identify the affected tuple or tuples.

4

Check

Confirm the intended rows changed.

Updating several matching tuples deliberately

A condition may intentionally match more than one tuple:

UPDATE Sensor
SET SampleInterval = 20
WHERE SensorType = 'Air quality';

This changes all three air-quality sensor tuples. The statement is valid only when that wider change is the intended result.

Updating more than one attribute

UPDATE ResearchSite
SET RegionCode = 'W',
    Active = TRUE
WHERE SiteID = 'RS424';
Common mistake: Without a WHERE clause, every tuple in the target table may be updated.
Exam tip: When exactly one tuple should change, using a primary-key condition is usually the clearest choice because the key identifies one tuple uniquely.

Removing Tuples with DELETE FROM

DELETE FROM removes complete tuples. The WHERE clause identifies which tuples are to be removed.

DELETE FROM Sensor
WHERE SensorID = 'S-218';

This deletes one sensor tuple because SensorID is the table's primary key.

Deleting several matching tuples

DELETE FROM Sensor
WHERE SiteID = 'RS205';

This removes every sensor whose foreign key is 'RS205'. In the current data, two tuples match.

Restricted deletion

DELETE FROM Sensor
WHERE SensorID = 'S-218';

Only the matching tuple is removed.

Unrestricted deletion

DELETE FROM Sensor;

Every tuple in Sensor may be removed.

Common mistake: DELETE FROM Sensor; does not mean β€œdelete one sensor.” With no condition, it targets all tuples in the table.
Do not confuse: DELETE FROM removes tuples but leaves the table definition in place. Removing a table structure is a different DDL operation and is outside this lesson.

Referential Integrity and Operation Order

A parent tuple cannot usually be deleted while child tuples still reference its primary key. The dependent sensor tuples must be dealt with first.

Blocked order

  1. Delete ResearchSite RS205.
  2. Two Sensor tuples still contain SiteID = 'RS205'.
  3. The DBMS rejects the parent deletion.

Safe order

  1. Delete the Sensor tuples that reference RS205.
  2. Delete ResearchSite RS205.
  3. No dangling foreign-key references remain.
DELETE FROM Sensor
WHERE SiteID = 'RS205';

DELETE FROM ResearchSite
WHERE SiteID = 'RS205';
Relationship rule:
Insertion normally proceeds parent β†’ child.
Deletion normally proceeds child β†’ parent.
Common mistake: A foreign key does not have to be unique, so several child tuples may need to be removed before one parent tuple can be deleted.

A Safer Maintenance Workflow

SQL maintenance statements can affect permanent stored data. A disciplined process reduces accidental changes.

1

Interpret

Identify the intended table, attributes and tuples.

2

Check keys

Consider primary-key uniqueness and foreign-key references.

3

Preview

Use the same condition in a SELECT query to see which tuples match.

4

Execute

Run the INSERT, UPDATE or DELETE statement.

5

Verify

Check that the resulting table state matches the requirement.

Previewing an update condition

SELECT SensorID, SensorType, SampleInterval
FROM Sensor
WHERE SensorType = 'Air quality';

If the preview returns three tuples, the corresponding update condition will affect those same three tuples.

Exam tip: Before finalising an UPDATE or DELETE, say in words which tuples the WHERE condition matches. This exposes overly broad conditions.

Worked Example: Retiring a Site and Adding Its Replacement

Estuary Point is being retired. Remove its sensors and then remove the site. Add a new site called River North with identifier RS640, region code N and active status TRUE. Finally, install sensor S-950 at the new site to measure water flow every 15 minutes, checked daily at 07:45 from 12 August 2026.

Step 1: Remove child tuples

DELETE FROM Sensor
WHERE SiteID = 'RS205';

Step 2: Remove the parent tuple

DELETE FROM ResearchSite
WHERE SiteID = 'RS205';

Step 3: Insert the new parent tuple

INSERT INTO ResearchSite
    (SiteID, SiteName, RegionCode, Active)
VALUES
    ('RS640', 'River North', 'N', TRUE);

Step 4: Insert the new child tuple

INSERT INTO Sensor
    (SensorID, SiteID, SensorType, InstalledOn,
     DailyCheckTime, SampleInterval)
VALUES
    ('S-950', 'RS640', 'Water flow', '2026-08-12',
     '07:45', 15);
Exam tip: When several statements are required, explain the dependency between them. Here, old child tuples are deleted before their parent, and the new parent is inserted before its child.

Interactive: Data Maintenance Simulator

Test insertions, trace update conditions and explore how referential integrity affects deletion order.

Test an INSERT statement

Primary keyNot checked
Foreign keyNot checked
Data typeNot checked
Select a proposed insertion and run the checks.
SensorID SiteID SensorType SampleInterval

Common Mistakes and Misconceptions

  • Supplying a different number of values from the number of named attributes.
  • Putting values in an order that does not match the attribute list.
  • Inserting a duplicate primary-key value.
  • Inserting a child tuple whose foreign key has no matching parent tuple.
  • Using quotation marks around an integer or omitting them around text.
  • Writing UPDATE without SET.
  • Using a condition that matches more tuples than intended.
  • Omitting WHERE from an update or deletion.
  • Deleting a parent tuple while child tuples still reference it.
  • Confusing DELETE FROM with removal of the table definition.

Practice

Original practice tasks

Use the following tables:

Garden(GardenID, GardenName, Zone, Open)

Device(DeviceID, GardenID, DeviceType, InstalledDate, CheckTime, IntervalMinutes)

  1. Write an insertion for garden G70, called East Terrace, in zone E, with Open = TRUE.
  2. Insert device D814 at garden G70. It is a soil sensor installed on 2026-09-14, checked at 06:30 every 25 minutes.
  3. Explain why the device insertion should follow the garden insertion.
  4. Write an update that changes D814's interval to 15 minutes.
  5. Write an update that closes every garden in zone N.
  6. Explain the effect of omitting WHERE from the previous statement.
  7. Write a deletion that removes device D814 only.
  8. Write the two statements needed to remove garden G70 and all devices that reference it, in a safe order.
  9. Explain why this insertion may fail:
    INSERT INTO Device
        (DeviceID, GardenID, DeviceType, IntervalMinutes)
    VALUES
        ('D920', 'G99', 'Humidity', 10);
  10. Correct the following statement:
    UPDATE Device
    IntervalMinutes = 12
    WHERE DeviceID 'D814';
  11. Explain the difference between these statements:
    DELETE FROM Device
    WHERE GardenID = 'G70';
    
    DELETE FROM Device;

Review

Statement Purpose Main safety check
INSERT INTO ... VALUES Adds a new tuple. Match attributes, values, data types and key constraints.
UPDATE ... SET ... WHERE Changes values in matching tuples. Confirm exactly which tuples the condition selects.
DELETE FROM ... WHERE Removes matching tuples. Check the condition and any foreign-key dependencies.
Parent β†’ child insertion Creates a referenced tuple before its dependent tuple. The child foreign key must already have a valid target.
Child β†’ parent deletion Removes references before the referenced tuple. Avoid breaking referential integrity.
Final exam tip: For every maintenance statement, identify the target table, the values involved, the affected tuple or tuples and any relevant key constraint.