We investigate more advanced queries, SQL INSERT and DELETE commands and the use of subqueries. We will work with the same database as last week at
SELECT Surname
FROM Officer
WHERE RegNum IN
(SELECT Officer
FROM Offence
WHERE CarReg IS NULL);
Then from the View menu choose Datasheet View and you should obtain the table
| Surname |
| Burke |
This is an example of a subquery. The last three lines (in round brackets) form one query (which is evaluated first). The result of this query is used in the main query in the first 3 lines. This is often also called a nested query. The operation can also be carried out using various JOIN commands, but subqueries are popular since they are often easier for humans to read.
INSERT INTO Officer
VALUES(319,'Thumb','Tom');
If you view this query under Datasheet View you should see something like
| Expr1000 | Expr1001 | Expr1002 |
| 319 | Thumb | Tom |
Save this Query (choose "Save As" under "File" menu), picking a name yourself. This INSERT Query should insert one extra row in the Officer table. Close the Query, then re-open it by double clicking on the icon. It will warn you that it is about to add to the table: click Yes, and then Yes again. Now open the Officer table separately and check the row has indeed been added. Once again double click on the INSERT Query, and answer Yes to the questions. Does INSERT add the row this time? Why not?
DELETE *
FROM Officer
WHERE Officer.Firstname = 'Tom';
Under Datasheet View this should look like
| RegNum | Surname | Firstname |
| 319 | Thumb | Tom |
Save this query, and then re-open it by double clicking. Answer Yes to the questions (ACCESS will warn you it is about to delete records). Check the Officer table to verify the record has been removed. Try to run the same query a second time. Do any deletions occur this time?