3 4
Entering new records is only one part of maintaining reliable data. Often, you’ll need to adjust existing data. Updates are generally made to individual records, but occasionally you might need to modify an entire group of records. Can you imagine making the same change to hundreds or even thousands of records? An update query is the way to go when you’re dealing with large groups of records. Update queries allow you to state your change in the form of criteria, and then the query takes care of the entire group.
For example, suppose you want to increase the price of each product listed in the Northwind database by 2 percent. You could spend a lot of time calculating new prices for all 77 products in the Products table and then modify every record yourself. A better idea is to run a query.
note
To run an update query on the Northwind database, follow these steps:
[UnitPrice]+([UnitPrice]*0.02)
note
Figure 10-2. This expression modifies the existing entry.
This expression evaluates to the original value of UnitPrice plus 2 percent of UnitPrice. Be sure to include the brackets, or Access will interpret the field name as a string and delete the data in the UnitPrice field when you run the query.
Access won’t display the new values; it displays only those values that the query will update. In this case, that includes all the values in the UnitPrice field.
Figure 10-3. Click Yes to update the Products table.
Figure 10-4. The query modified each UnitPrice value.
tip
Update queries use the SQL UPDATE statement to modify data. The UPDATE statement takes the following form:
UPDATE table/query SET col = expression WHERE criteria;
You can update multiple fields in an update query by setting appropriate criteria expressions for each field that needs updating. In this case, the query’s SQL statement will have multiple SET clauses, separated by commas.
Let’s take a look at the update query’s SQL statement from the previous example:
UPDATE Products SET Products.UnitPrice = [UnitPrice]+([UnitPrice]*0.02);
The UPDATE keyword is the first clue that you’re dealing with a query that modifies existing data. The SET clause identifies the field that the query updates and the field’s new value.
Updating data won’t always be as clear-cut as in the previous example. Sometimes you need to update some records but not others. For instance, suppose you want to increase the price of only those products that cost less than $20. You can still use an update query, but you have to be specific about which records the query should modify. You do this by adding a criteria expression.
Follow these steps to create a query that contains a criteria expression:
<20
Figure 10-5. The Criteria expression restricts updates to those records in which the unit price is less than $20.
As you can see in Figure 10-6, Chef Anton’s Cajun Seasoning (the fourth record) is the first record the query didn’t update because the UnitPrice value is $22, which is greater than $20.
Figure 10-6. This query modified only some of the UnitPrice values.
Review this query’s SQL statement, shown here:
UPDATE Products SET Products.UnitPrice = [UnitPrice]+([UnitPrice]*0.02) WHERE (((Products.UnitPrice)<20));
You can see that this SQL statement is quite similar to our first query’s SQL statement. However, this one includes a WHERE clause that restricts the query.
Update queries work just as well with multiple tables. For example, the following SQL statement updates the "many" side of the join based on criteria in the "one" side:
UPDATE Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID SET [Order Details].Discount = 0.1 WHERE Orders.CustomerID = "CHOPS";
Specifically, the query states that when the CustomerID value equals "CHOPS", update the Discount value to 0.1 (10 percent).
note
Updating Both Sides of a Relationship
Relationships play a part in the update process, even though this part isn’t always readily apparent. In fact, you might not need an update query at all if the correct relationships exist. When updating records in a one-to-many relationship, you typically update many records. However, the perspective changes a bit if you need to change both sides of that relationship. For instance, let’s suppose you want to change a primary key value. Naturally, you’d also need to update all the related records—that is, the foreign key values. You can approach this situation in two ways. You can change the primary key value and then run an update query to modify the related table (foreign key values) accordingly.
The second approach is to create a relationship (if none exists) between the related fields in the Relationships window and select the Enforce Referential Integrity and Cascade Updated Related Fields check boxes in the Edit Relationships dialog box. (See Chapter 4, "Creating a Database," for more details on setting options in the Edit Relationships dialog box.) Access will automatically update the foreign key field in the related table when you change the primary key value. Remember that the cascading updates solution works only when you update the primary and foreign key values. (Keep in mind that you can’t update the values in an AutoNumber field.)
For definitions of database terms, see Chapter 3, "Introduction to Database Design."