R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Using update queries in a database

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

Mark Whitehorn, Personal Computer World 21 Jun 2006
 > View picture gallery
ADVERTISEMENT

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.


All Software Developer
Tags: Databases

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
Solihull, United Kingdom | Enzen Global Limited
Business Consultant - £35,000 - £40,000 - Solihull We are in need of a Business Consultant with strong analytical skills and a penchant for learning the domain knowledge of the Utilities sector (Gas industry in ... more >
Welwyn Garden City, Hertfordshire, United Kingdom | Tesco.com
Affiliate & Media marketing manager - Welwyn Garden CityWho's behind the world's most successful online retailer? Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales ... more >
Welwyn Garden City, Hertfordshire, United Kingdom | Tesco.com
Infrastructure Delivery Project Manager - Welwyn Garden City Who's behind the world's most successful online retailer? Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales ... more >
Cardiff, United Kingdom | University of Wales
Projects Officer - £26,665 - £30,912 - Cardiff The Projects Officer will work on specific projects under the direction of the Head of Information Services. It is expected that these will concentrate on the redevelopment ... more >
More job opportunities