GEOG 897D
Spatial Database Management

Deleting Records

PrintPrint

When working in Access deleting records can be as easy as opening the table, navigating to the desired records, selecting them using the gray selection boxes to the left of the records, and hitting the Delete key on the keyboard. Likewise, it is possible to follow the same methodology to delete records returned by a SELECT query (if it's a one-table query).

However as with the other query types, it is sometimes necessary to write a query to perform the task. For example if you've ever canceled a hotel reservation or an order from an online vendor, a DELETE query was probably executed behind the scenes. Let's see how such a query can be built using the Access GUI and examine the resulting SQL.

A. Deleting records

Let's say a revised version of the 1001 albums list is released and the albums recorded by The Who on the Track Records label have dropped off the list. Knowing that Track Records no longer has albums on the list, you decide to delete it from the Labels table.

  1. Select Create > Query Design.
  2. Double-click on Labels to add it to the query design GUI, then close the Show Tables dialog.
  3. Change the query type to Delete. As with the Update query, the Sort and Show rows disappear from the design grid, replaced by a Delete row.
  4. Double-click * to add it to the design grid. You should see the keyword From appear in the Delete row of the design grid.

    While it's possible to bring down an individual field instead of *, which implies that it might be possible to delete values in a particular field only, that's not really the case. Delete queries are used to delete entire rows meeting certain criteria. If you want to wipe out the values in a field, leaving other field values intact, you should use an Update query.
  5. Double-click Label_Name to add it to the design grid. You should see the keyword Where appear in the Delete row of the design grid.
  6. In the Criteria cell, enter "Track Records".
  7. Execute the query.
  8. You should be asked to confirm that you really want to delete the 1 record meeting your criteria. Answer Yes to go ahead with the deletion.
  9. Open the Labels table and verify that the record has been deleted.
  10. Open the Albums table and note that The Who Sell Out and Tommy have been deleted from that table also. This deletion was carried out because of the Cascade Delete Related Records setting we made earlier. If we had not checked that box the two albums would be left in the Albums table referring to a record label that does not exist in the Labels table.
  11. Return to the query and select View > SQL View. Note the statement produced by the GUI:

    DELETE Labels.*, Labels.Label_Name
    FROM Labels
    WHERE (((Labels.Label_Name)="Track Records"));

    Again, this statement could be simplified as follows:
    DELETE *
    FROM Labels
    WHERE Label_Name="Track Records";
  12. Close the query without saving.

One final note before moving on to the graded assignment. I had you delete the record label from the database because I wanted to show you how to write a DELETE query. However, think about other actions you might have taken instead under that scenario. Depending on the circumstance, it might make more sense to leave that label in the database and delete only the albums (in case the label once again has an album on the list). The upside of deleting the label is that answering a question like, "Which record companies released the albums on the list?" requires dealing only with the Labels table. On the other hand, if the Labels table included companies that had no albums on the list, answering that question would require a more complex query, involving a join between Albums and Labels.

Another response to the revised-list scenario might be to make it possible for the database to store all versions of the list rather than just its current state. Generally speaking, it's better to err on the side of keeping data versus deleting. Doing so makes your database capable of answering a larger array of questions; it's quite common for database users to come up with questions that could not have been predicted in the initial design. The downside to this decision is that it necessitates changes to the database design. What changes would be required in this scenario?