Query by Example QBE Tutorial
Query by Example QBE Tutorial
Lesson 10: Basic queries using applications to have different views of the same
data.
QBE
10.2 Learning objectives
10.1 Introduction: Using queries to get the ! create different types of queries
information you need ! understand how queries can be used to
At first glance, it appears that splitting answer questions
information into multiple tables and ! develop a naming convention for
relationships creates more of a headache than queries
it is worth. People generally like to have all the ! understand the difference between an
information they require on one screen (like a “updatable” and “non-updatable”
spreadsheet, for instance); they do not want to recordset
have to know about multiple tables, foreign
keys, relationships, and so on.
10.3 Exercises
Saved queries address this problem. Queries
allow the user to join data from one or more 10.3.1 Creating a query
tables, order the data in different ways,
calculate new fields, and specify criteria to ➨ Use the New button in the Queries pane of
filter out certain records. The important thing the database window to create a new query
to keep in mind is that a query contains no as shown in Figure 10.1.
data—it merely reorganizes the data from the
table (or tables) on which it is built without ➨ Add the Products table to the query as
changing the “underlying tables” in any way. shown in Figure 10.2.
Once a query is defined and saved, it can be
used in exactly the same way as a table. ➨ Examine the basic elements of the query
Because of this, it is useful to think of queries design screen as shown in Figure 10.3.
as “virtual tables”. Indeed, in the majority of
DBMSes, saved queries are called views because ➨ Save your query (Ctrl-S) using the name
they allow different users and different qryBasics.
Basic queries using QBE Exercises
2 o f 16
1 Select the
Queries tab in 2 Press the New
button to create a
the database new query.
window.
The queries you build in these exercises fields. The operations are fundamental in the
? are for practice only. That is, they are not sense that they are supported by all relational
used in your order entry project and it is database systems.
not absolutely critical that they be saved
as part of your database. On the other 10.3.2.1 Projection
hand, there is little reason not to save
Projecting a field into a query simply means
them.
including it in the query definition. The ability
to base a query on a subset of the fields in an
10.3.2 Five fundamental query operations underlying table (or tables) is particularly
In the following sections, you are introduced to useful when dealing with tables that contain
five fundamental query operations: projection, some information that is confidential and some
selection, sorting, joining, and calculated that is not confidential.
Basic queries using QBE Exercises
3 o f 16
FIGURE 10.2: Add tables to your query using the “show table” dialog.
2 Press Close
when done.
For instance, the PAY_EMPS table you used in have no way of viewing sensitive pay
Section 8.3.3 contains a field describing the information.
employee’s pay level. If you created a saved
query that projected all the employee fields Recall that a view called SALES_REPS was
except EMP_PAY_LE and gave users permission ? used in Lesson 9 to show only the
to view the saved query instead of the employees names and ID numbers from
PAY_EMPS table, then unauthorized users would the PAY_EMPS table.
Basic queries using QBE Exercises
4 o f 16
FIGURE 10.4: Project a subset of the available fields into the query definition.
select query, the resulting recordset ➨ Use complex selection criteria to answer
contains only those records that satisfy the more complex questions:
criteria.
• “Which products cost less than $5 each?”
(see Figure 10.7)
10.3.2.3 Complex selection criteria
• “Which products cost less than $2 each or
It is also possible to create complex selection
cost less than $5 for unit sizes greater than
criteria using Boolean (logical) constructs such
one?” (see Figure 10.8)
as AND, OR, and NOT.
Basic queries using QBE Exercises
6 o f 16
10.3.2.5 Joining
2 Enter the criterion in the appropriate row.
The query will return all records that In Lesson 7, you were advised to break you
satisfy the condition UnitPrice > 20. information down into multiple tables with
relationships between them. In order to put this
information back together in a usable form, you
use a join query.
10.3.2.4 Sorting
When you use a query to sort, you do not ➨ Save and close qryBasics.
change the physical order of the records in the
underlying table (that is, you do not sort the ➨ Open the relationships window and ensure
table). As a result, different queries based on you have a relationship declared between
the same table can display the records in a Customers and Regions. If not, declare one
different sequence. and ensure referential integrity is enforced
(review Section 7.3 as required).
Basic queries using QBE Exercises
7 o f 16
➨ Create a new query called qryJoin based When a query is based on more than one
on the Customers and Regions tables. ? table, it is often the case that certain
field names are used in multiple tables
➨ Project Regions.RegionName, (e.g., RegionCode is used in both
Customers.CustName, and Customers.City Customers and Regions). To eliminate
as shown in Figure 10.9. ambiguity, the
<table name>.<field name> notation is
used to refer to fields.
Basic queries using QBE Exercises
8 o f 16
returned since there is no record in the name instead of the single-letter RegionCode
Regions table with a RegionCode = NULL. field. The result is similar to the “monolithic”
table design discussed in Section 7.1.1.
10.3.3 Using queries to edit records However, there are some important differences,
as you will see in the following exercises.
Once ACCESS knows the RegionCode of a
customer, it can uniquely identify the region to
which the customer has been assigned. This
allows us to show the more user-friendly region
Basic queries using QBE Exercises
10 o f 16
10.3.3.1 Editing a record on the “many” side record selector to save the record, you will
notice that the value of the RegionName
➨ Add Customers.RegionCode to your query. field changes automatically.
Ensure you add Customer.RegionCode, not
Regions.RegionCode. 10.3.3.2 Edit a record on the “one” side
➨ Switch to datasheet view, as shown in ➨ Click on one of the “Key” values in the
Figure 10.10. RegionName field and change it to a more
descriptive value “Key Accounts”, as shown
➨ Change the RegionCode value for ROSCH DRY in Figure 10.11.
GOODS from “E” to “K” (assume the firm is
being transferred to the special “key ➨ Save the record and notice how the change
accounts” region). When you click on the propagates to all key account regions.
FIGURE 10.10: Use a join query to make a change to a record on the “many” side of the relationship.
1 Add Customers.RegionCode to
the query.
FIGURE 10.9: Create a query that joins FIGURE 10.11: Use a join query to make a
Customers and Regions. change to a record on the “one” side of the
relationship.
Since the purpose of this query is to add following guidelines should be appended to
! records to the Customers table, it is clear those introduced in Section 5.4.5.1:
that you need to project all of the
• Use meaningful names — An object named
customer fields. The asterisk (*) provides
Table1 does not tell you much about the
a convenient means of doing this, even if
contents of the table. Furthermore, since
the fields in the table change over time.
there is no practical limit to the length of
the names, you should not use short, cryptic
➨ Switch to datasheet view and add a new names such as s96w_b. As the number of
customer record (make one up).
objects in your database grows, the time
spent carefully naming your objects will pay
➨ Note that when the RegionCode is itself back many times.
specified, the name of the region is
“looked-up” and filled in automatically. • Give each type of object a distinctive
This is what is meant by feedback—the prefix (or suffix) — This is especially
additional region information helps you important in the context of queries since
determine whether you have entered the tables and queries cannot have the same
correct RegionCode. name. For example, you cannot have a
table named Orders and a query named
10.4 Discussion Orders. However, if all your query names
are of the form qryOrders, then
distinguishing between tables and queries is
10.4.1 Naming conventions for database
straightforward.
objects
Table 10.1 shows a suggested naming
As discussed in the section on field names in
convention for ACCESS database objects (you will
Section 5.4.5.1, there are relatively few naming
discover what these objects are in the course of
restrictions for database objects in ACCESS.
doing the tutorials).
However, a clear, consistent method for
choosing names can save time and avoid
confusion later on.
10.4.2 Using queries to populate tables on the
“many” side of a relationship
Although there is no hard and fast naming
convention required for the project, the In Section 10.3.4, you added a record to the
Customers table to demonstrate the automatic
Basic queries using QBE Discussion
13 o f 16
2 Switch to datasheet
view and attempt to add
another new customer Only a subset of the
record. ? customer fields are
projected in order to
keep this illustration
simple.
relationship. As you will probably discover, ➨ Delete the RegionCode relationship and
however, there are many different ways to project a couple of fields from both tables,
generate nonsensical queries. as shown in Figure 10.13.
Later, when building forms, you may 10.5 Application to the assignment
! accidentally base a form on a non-
updatable recordset. You then may spend ➨ Create a query called qryOrderDetails
a great deal of time trying to get the form that joins the OrderDetails and Products
to work when the real problem is the tables. When you enter a valid ProductID,
underlying query. A quick check for a the information about the product (such as
“new record” row in all your new queries name, quantity on hand, and so on) should
can save time and frustration later on. appear automatically.
Basic queries using QBE Application to the assignment
16 o f 16
Due to the referential integrity have in inventory), and determine the default
? constraints you specified in Lesson 7, you selling price of each good.
will not be able to save any records you
use to test qryOrderDetails until you HINT: Much of the effort involved in switching
have a corresponding OrderID in the between tables is eliminated if you use
Orders table. At this point, you can the qryOrderDetails query you created
simply hit the Esc key to discard the test above for this exercise.
data without saving it.
Entering orders into your system will be
If the name of the product does not ? much less work once the input forms and
! appear when you enter a valid event-driven programs are in place. The
ProductID, you have used the wrong goal at this point is to get you thinking at
ProductID field. Review Section 10.4.2 a very specific level about the order entry
and ensure you understand (and fix) the process and what needs to be automated.
error before continuing.