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 INTOstatement 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 ... WHEREstatement to change selected tuples. - Predict the effect of omitting or widening an update condition.
- Write a
DELETE FROM ... WHEREstatement to remove selected tuples. - Explain why a missing
WHEREclause 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.
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)
Sensor
Sensor(SensorID, SiteID, SensorType, InstalledOn,
DailyCheckTime, SampleInterval)
Current ResearchSite data
| SiteID | SiteName | RegionCode | Active |
|---|---|---|---|
| RS101 | Forest Edge | NW | TRUE |
| RS205 | Estuary Point | SE | TRUE |
| RS318 | Hill Reserve | NW | TRUE |
| RS424 | Wetland South | SW | FALSE |
Current Sensor data
| SensorID | SiteID | SensorType | InstalledOn | DailyCheckTime | SampleInterval |
|---|---|---|---|---|---|
| S-104 | RS101 | Air quality | 2026-02-14 | 07:30 | 10 |
| S-218 | RS205 | Soil moisture | 2025-09-03 | 06:45 | 30 |
| S-311 | RS101 | Air quality | 2026-01-27 | 08:00 | 15 |
| S-405 | RS424 | Water level | 2024-11-19 | 05:50 | 5 |
| S-522 | RS318 | Temperature | 2025-06-08 | 09:15 | 20 |
| S-607 | RS318 | Air quality | 2024-04-22 | 07:10 | 60 |
| S-714 | RS205 | Water level | 2026-03-12 | 06:20 | 10 |
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);
Sensor
The six named Sensor 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 |
| SampleInterval | 12 | Integer without quotation marks |
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);
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';
UPDATE
Choose the target table.
SET
Define the replacement value.
WHERE
Identify the affected tuple or tuples.
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';
WHERE clause, every tuple in
the target table may be updated.
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.
DELETE FROM Sensor; does not mean
βdelete one sensor.β With no condition, it targets all tuples in the table.
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
- Delete
ResearchSite RS205. - Two Sensor tuples still contain
SiteID = 'RS205'. - The DBMS rejects the parent deletion.
Safe order
- Delete the Sensor tuples that reference
RS205. - Delete
ResearchSite RS205. - No dangling foreign-key references remain.
DELETE FROM Sensor
WHERE SiteID = 'RS205';
DELETE FROM ResearchSite
WHERE SiteID = 'RS205';
Insertion normally proceeds parent β child.
Deletion normally proceeds child β parent.
A Safer Maintenance Workflow
SQL maintenance statements can affect permanent stored data. A disciplined process reduces accidental changes.
Interpret
Identify the intended table, attributes and tuples.
Check keys
Consider primary-key uniqueness and foreign-key references.
Preview
Use the same condition in a SELECT query to see which tuples match.
Execute
Run the INSERT, UPDATE or DELETE statement.
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.
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
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);
Interactive: Data Maintenance Simulator
Test insertions, trace update conditions and explore how referential integrity affects deletion order.
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
UPDATEwithoutSET. - Using a condition that matches more tuples than intended.
- Omitting
WHEREfrom an update or deletion. - Deleting a parent tuple while child tuples still reference it.
- Confusing
DELETE FROMwith 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)
-
Write an insertion for garden G70, called East Terrace, in zone E, with
Open = TRUE. - Insert device D814 at garden G70. It is a soil sensor installed on 2026-09-14, checked at 06:30 every 25 minutes.
- Explain why the device insertion should follow the garden insertion.
- Write an update that changes D814's interval to 15 minutes.
- Write an update that closes every garden in zone N.
-
Explain the effect of omitting
WHEREfrom the previous statement. - Write a deletion that removes device D814 only.
- Write the two statements needed to remove garden G70 and all devices that reference it, in a safe order.
-
Explain why this insertion may fail:
INSERT INTO Device (DeviceID, GardenID, DeviceType, IntervalMinutes) VALUES ('D920', 'G99', 'Humidity', 10); -
Correct the following statement:
UPDATE Device IntervalMinutes = 12 WHERE DeviceID 'D814'; -
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. |