CIE AS & A Level Computer Science, section 8.3 (Data Definition Language (DDL) and Data Manipulation Language (DML))
Data maintenance
DML statements that change the data in tables are called data maintenance statements.
INSERT INTO
Adds a new record.
INSERT INTO Member (MemberID, Name, Year)
VALUES (4, 'Dev', 13);
Text and dates are written in single quotes, and numbers are not.
UPDATE
Changes values in existing records. Always use WHERE to choose which records to change.
UPDATE Book
SET Price = 8.0
WHERE BookID = 10;
Without WHERE, every record would be changed.
DELETE FROM
Removes records.
DELETE FROM Loan
WHERE LoanID = 103;
Without WHERE, all records in the table would be deleted.
Referential integrity
The DBMS can reject changes that would break the links between tables. You cannot delete a member who still has loans, or insert a loan for a member who does not exist (if foreign keys are enforced).
Worked example
INSERT INTO Member ...makes the member count go from 3 to 4.UPDATE Book ... WHERE BookID = 10changes the price of Dune to 8.0.DELETE FROM Loan WHERE LoanID = 103removes one loan, leaving 3.
Common mistake: leaving out the WHERE clause in UPDATE or DELETE FROM, which changes or removes every record. Also, use quotes round text values and dates but not round numbers.
Exam tip: write the statement with the table name, the fields and the values in the same order. Check that the WHERE clause picks only the records you intend.
Check yourself
Write SQL to remove the member with MemberID 4.
DELETE FROM Member WHERE MemberID = 4;
What happens if UPDATE is used without WHERE?
Every record in the table is changed.
Write SQL to set the Year of the member named Ben to 14.
UPDATE Member SET Year = 14 WHERE Name = 'Ben';