Simple clear advice in plain English

Hands on: Using update queries in a database

Make changes to your data; and a relational database speed fallacy exposed

We often use queries to extract subsets of data, but they can do so much more.

Perhaps you want to increase by five per cent the price for all items with a product code starting with ‘KP’, or you want to delete all records where the customer’s name is ‘Smith’.

These operations don’t just return what’s already in the tables; they perform some action on the data, making changes to what’s stored in the database.

It’s a slight oddity of the relational model that such operations are treated as queries.

In Access, for example, the Query Type button on the menu bar offers a dropdown list of six query types: Update is the one we’ll concentrate on here.

Mark Thornton emailed on this topic: he has a table of 100 rows and wants to update the values in Field A by adding the value in Field B to them.

In another table of 3,000 rows, he needs to decrement the value in Field C by one.

These are perfect operations for illustrating the joys of, in SQL terminology, Update queries. (The standard queries that just return a set of data are known as Select statements or queries.)

Imagine we are working with a 100-row version of the Item table in screen 1 .

If you are doing this for real, you will of course be working on a copy of the table: update queries change data so it’s important you check that updates behave in the manner you require before performing them on your tables.

We want to add the value in Field B to that in Field A, which would, for the row with ID 4, give us the value of 4 in Field A.

Open a query grid as usual and add the Item table, including Field A. Now click the down arrow alongside the Query Type button (showing the same glyph as the Query tab in the database window) and select Update Query.

The query grid changes to give a row labelled ‘Update To:’. In the Update To row for Field A, enter:

[Field A]+[Field B]

which simply says add the contents of the two fields ( see screen 2 ). The SQL reads:

UPDATE Item SET Item.[Field A] = [Field A]+[Field B];

Clicking the View button shows the existing values in Field A, which will be updated when we run the query.

To perform the update, return to the query grid and click the Run button (red exclamation mark), whereupon Access warns that you’re about to update rows and that, once performed, updates cannot be reversed with the Undo facility.

Click OK to go ahead. Inspect the table ( see screen 3 ), and the Field A values have indeed been updated as required.

The second operation – decrementing a value by 1 – is performed using another Update query, shown in screen 4, and the SQL is:

UPDATE Item SET Item.[Field C] = [Field C]-1;

Access issues a warning when you run the query, and when you go ahead the table is updated as required ( see screen 5 ). Download the DBCJUL06.MDB database.

Mark says he’s presently working with a solution that uses VBA querying, but it is very slow. That’s not surprising: VBA provides a sequential, procedural solution that will pull out a single row, do whatever is necessary and then move on to the next row.

Because it handles rows one at a time, it is indeed slow. Writing the same queries in SQL adds speed – lots of it – because SQL is a set-based language.

It is designed to handle sets of data; whole bunches of records at once rather than a record at a time. You give SQL a set, specify what you want done, and it’s carried out on the whole set at a single stroke.

Of the query types offered by the Query Type list, Update, Delete, Append and Make-Table are all known as ‘action queries’ in Access, because they perform some action on your data.

Article tags

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Image gallery Hands on Databases July 2006

Tips for Word illustration

Get the best out of Microsoft Word

Unless you've had a training course on Word, you probably know and use only a small percentage of its capabilities. Here are 10 top features for you to try out

Treesize Free tip 1 screenshot

How to prevent PC problems

Follow our useful tips to solve common problems using Windows or free software

Question & Answer

Q.How do I store musician and other information about...

> Read the answer

Q.Why can't my browser find the website address I typed...

> Read the answer

Q.All updates have been downloaded, so why won't Windows...

> Read the answer

Best deals on the web

img

Apple MacBook Pro (MC724LL/A)

£999.99- Buy it now

img

Sony Vaio VPCF23P1E/B

£679.98- Buy it now

img

Samsung 300E5A-A01DX

£449.99- Buy it now

Great benefits for subscribers!

Most popular articles

Poll

Which is your preferred web browser

Jargon Buster

Computing terms explained in plain English

VoIP

Voice over IP. The routing of voice conversations over the internet, which is cheaper than the telephone...

Great shopping deals from Computeractive