Sei sulla pagina 1di 3

1.

Queries - computed tables

Highlights
. Combine multiple tables into one (Query).
. Basics of Structured Query Language (SQL).
. User-entered search criteria.

In this chapter we will begin making a functional pro-totype that can show real data and update it. To do this, we
often have to combine data from many tables in the database. All relational database systems provide means for
combining tables into new tables by means of queries. In this chapter we will see how to do it in Access.

4.1 Query: join two tables

In Chapter 3 we had a simple version of the Find Guest window. It showed a list of guests based on data from the
Guest table. In the real system we want to show a list of stays, including data about the guest and the room booked.
This means that we have to combine data from the Stay table, the Guest table, and the RoomState table.
Our first version of the stay list will just combine the Stay table and the Guest table. The bottom of Figure 4.1 shows
the result we want: a single table with some fields from tblStay and some from tblGuest.
Create a query
1. Start in the database window. Select Queries and Create query in Design view. (In Access 97 select New and
then Design view.)
2. Access asks you to select the tables you want to combine. Select tblGuest and tblStay (Figure 4.1). Click Add
and then Close.

You now see the query design window (middle of Fig-ure 4.1). The top part of the window is an E/R-model, in our
case consisting of tblGuest and tblStay. Access has included the relationship from the full E/R-model. It shows that
the tables will be combined according to guestID. This is just what we want in our case, but in other cases you have
to remove the relationships you don't need and add new ones that you need for the query. These changed
relationships are only used in the query; they don't influence the full E/R-model.
You may delete tables from the query window or add further tables by right-clicking in the E/R-model.
In the lower part of the window, you see the query grid where we will make a column for each field in the
computed table.
1. Drag stayID from tblStay to the grid. Then drag name, address1 and phone from tblGuest. Finally, drag state
from tblStay. (You may also double-click the fields.)

You may rearrange the columns by selecting a column and dragging it to another place.
1. Switch to datasheet view. The query table should look like the bottom of the figure. It contains all stays
recorded in the database with guest informa-tion attached. In the example, John Simpson has three stays and
Yun Chen two stays.
2. Save the query and give it the name qryStayList. (The standard prefix for queries is qry.)

This looks almost too easy. What happens really? Access has made a so-called join of tblGuest and tblStay.
According to the E/R-model, each record in tblStay has a connecting string to a record in tblGuest. In the query
table, there will be one record for each of these strings. If one of the stays didn't have a string to a guest, this stay
would not occur in the result.
Since each query record corresponds to a string be-tween two source records, we can include arbitrary fields from
both source tables. This is what we have done.
Star = all fields? Note that the data model at the top of the query window has a star in each box. It means "all
fields". You may drag it to the grid and all source fields will be included in the query table. This may be con-
venient, but don't do it yet. Why? Assume that you drag the star from both tables. Then you will have two guestID
fields in the result. You then have to refer to them as tblStay.guestID and tblGuest.guestID. This leads to endless
confusion later, particularly because some of Access's built-in Wizards cannot figure out about these names and
screw things up.
Dynaset and data entry to query table
The query not only shows data, it can also be used for data entry. Try these experiments:
1. Open qryStayList and tblGuest at the same time.
2. In the query table, change the name of one of the guests. As soon as you move the cursor to the next line, you
will see the change in the query table as well as the guest table.
3. In tblGuest, try to change the guest name again. The query table will be updated immediately.

The query table we look at is a dynaset because it is updated automatically. We can enter data into it be-cause it is a
simple query where Access can find out how to store the data into the source tables. For more complex queries, this
is not possible.
52 4. Queries - computed tables
The query has a property called Recordset Type. It is Dynaset as a standard, but you can change it to Snap-shot.
Then Access computes the record list when you open the query, and doesn't update it dynamically. In this case you
cannot enter data through the query. (How to find the Recordset Property? From the query design window, use View
-> Properties. But don't change anything right now.)
Adding/deleting records in a dynaset
1. In the query table, enter a guest name in the last line (with star-indication). When you move the cursor up, you
have created a new guest record (but not a new stay record). You cannot see it in tblGuest, but if you close and
open tblGuest, you will see it. (Using the sort button A/Z on the tool-bar will also show the new guest.)
2. In the query table, enter a state in the last line and move the cursor up. Access refuses to do it. It tries to create a
stay record, but lacks the foreign key to the guest and cannot preserve the referential integ-rity. If you had
included the foreign key in the query, you could set it now and succeed. Never mind. (Remember to use Esc to
get out of the in-consistent data update.)
3. In the query table, try to delete a line. What you delete is the stay record, not the guest record.

Conclusion. The dynaset is suitable for editing data produced by a simple query. As soon as you fill in a field in the
new record line (star-marked), Access will try to create a new record. If you fill in a field from the guest table,
Access will make a guest record. If you fill in a field from the stay table, Access will (also) make a stay record.
When you move the cursor to another record, Access will check that referential integrity and other rules are met.
Fig 4.1 Query - join two tablesComputedtablesTablesto joinTablesto joinDrag wantedfields to grid.Drag the
wantedfields (Or double click)Wanted:List of stayswith guest dataDatasheetviewQuery grid

Potrebbero piacerti anche