AQA GCSE Computer Science (8525), section 3.7.2 (Structured query language (SQL))
INSERT INTO
Adds a new record to a table.
INSERT INTO Pupil (PupilID, Name, FormID, Mark)
VALUES (5, 'Ella', '10A', 70);
The values must be in the same order as the fields listed, and text goes in quotes.
UPDATE
Changes the data in existing records.
UPDATE Pupil
SET Mark = 75
WHERE PupilID = 2;
SETgives the new value (more than one field can be set, separated by commas).WHEREchooses which record(s) to change.
Warning: without WHERE, UPDATE changes every record in the table.
DELETE
Removes records from a table.
DELETE FROM Pupil
WHERE PupilID = 4;
Without WHERE, every record is deleted.
Summary
| Command | Purpose | Pattern |
|---|---|---|
INSERT INTO |
add a record | INSERT INTO table (fields) VALUES (values) |
UPDATE |
change a record | UPDATE table SET field = value WHERE condition |
DELETE |
remove a record | DELETE FROM table WHERE condition |
Keeping data safe
In real systems, deleting or updating without a careful WHERE can destroy data. Always check the condition, and make backups.
Common mistake: leaving out the WHERE in UPDATE or DELETE, which changes or removes every record. Also, the fields and values in INSERT INTO must be in the same order, and text values need quotes.
Exam tip: write each command in the exact pattern, with a semicolon at the end. For UPDATE and DELETE, say which record WHERE will pick (usually using the primary key).
Check yourself
What happens if you run DELETE FROM Pupil with no WHERE?
Every record in the Pupil table is deleted.
Write a command to change Ben’s mark (PupilID 2) to 50.
UPDATE Pupil SET Mark = 50 WHERE PupilID = 2;
Which keyword gives the new values in an UPDATE statement?
SET.