Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] - نسخه متنی

Alison Balter

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



Building Queries Based on Multiple Tables


If you have properly normalized your table data, you probably want to bring the data from your tables back together by using queries. Fortunately, you can do this quite easily with Access queries.

The query in Figure 4.10 joins the Customers, Orders, and Order Details tables, pulling fields from each. Notice that the CustomerID and CompanyName fields are selected from the Customers table, the OrderID and OrderDate from the Orders table, and the UnitPrice and Quantity from the Order Details table. After running this query, you should see the results shown in Figure 4.11. Notice that you get a record in the query's result for every record in the Order Details table. In other words, there are 2,155 records in the Order Details table, and that's how many records appear in the query output. By creating a multitable query, you can look at data from related tables, along with the data from the Order Details table.

Figure 4.10. A query joining the Customers, Orders, and Order Details tables.


Figure 4.11. The results of querying multiple tables.


Build a query that combines information from the Customers, Orders, and Order Details tables. To do this, build a new query by following these steps:


  • Select the Query tab from the Database window.

  • Click New.

  • Select Design view.

  • From the Show Table dialog box, select Customers, Orders, and Order Details by holding down the Ctrl key and clicking on each table name. Then select Add.

  • Click Close.

  • Some of the tables included in the query might be hiding below. If so, scroll down with the vertical scrollbar to view any tables that aren't visible. Notice the join lines between the tables; they're based on the relationships set up in the Relationships window.

  • Select the following fields from each table:

    Customers :
    Country, City

    Orders :
    Order Date

    Order Details :
    UnitPrice, Quantity

  • Sort by Country and then City in ascending order. Your finished query design should look like the one in Figure 4.12.

    Figure 4.12. The query design from the example.

  • Run the query. Data from all three tables should be included in the query output.


  • NOTE

    To remove a table from a query, click anywhere on the table in the top half of the query design grid and press the Delete key. You can add tables to the query at any time by clicking the Show Table button from the toolbar. If you prefer, you can select the Database window and then click and drag tables directly from the Database window to the top half of the query design grid.

    Pitfalls of Multitable Queries


    You should be aware of some pitfalls of multitable queries; they involve updating as well as which records you see in the query output.Chapter 3, "Relationships: Your Key to Data Integrity."

    For example, Figure 4.13 shows the result of a query based on the Customers, Orders, and Order Details tables. I have changed "Alfreds Futterkiste" to "Waldo Futterkiste" on a specific record of my query output. You might expect this change to affect only that specific order detail item. Pressing the down-arrow key to move off the record shows that all records associated with Alfreds Futterkiste have been changed (see Figure 4.14). This happened because all the orders for Alfreds Futterkiste were actually getting their information from one record in the Customers tablethe record for customer ID ALFKI. This is the record I modified while viewing the query result.

    Figure 4.13. Changing a record on the "one" side of a one-to-many relationship.


    Figure 4.14. The result of changing a record on the "one" side of a one-to-many relationship. Notice that the Company Name field has been updated for all records with ALFKI as the customer ID.


    To get this experience firsthand, try changing the data in the City field for one of the records in the query result. Notice that the record (as well as several other records) is modified. This happens because the City field actually represents data from the "one" side of the one-to-many relationship. In other words, when you're viewing the Country and City fields for several records in the query output, the data for the fields might originate from one record. The same goes for the Order Date field because it's also on the "one" side of a one-to-many relationship. The only field in the query output that can't be modified is TotalPrice, a calculated field. Practice modifying the data in the query result, and then returning to the original table and noticing which data has changed.

    The second pitfall of multitable queries is figuring out which records result from such a query. So far, you have learned how to build only inner joins. Join types are covered in detail in Chapter 11, but for now, it's important to understand that the query output contains only customers who have orders, and orders that have order detail. This means that not all the customers or orders may be listed. In Chapter 11, you'll learn how to build queries in which you can list all customers, regardless of whether they have orders. You'll also learn how to list only the customers without orders.

    Row Fix-Up in Multitable Queries


    The Row Fix-Up feature is automatically available to you in Access. As you fill in key values on the "many" side of a one-to-many relationship in a multitable query, the non-key values are automatically looked up in the parent table. Most database developers refer to this as

    enforced referential integrity . A foreign key must first exist on the "one" side of the query to be entered successfully on the "many" side. As you can imagine, you don't want to be able to add an order to your database for a nonexistent customer.Chapter 2, "What Every Developer Needs to Know About Databases and Tables." The customer ID associated with a particular order is actually being modified in the query. If the user adds a new record to the query, Access fills in the customer information as soon as the user selects the customer ID associated with the order.

    Figure 4.15. This query illustrates the use of Row Fix-Up in a query with multiple tables.


    Figure 4.16. The query result before selecting another customer ID.


    Figure 4.17. The result of an auto-lookup after the customer ID is changed. The information on the "one" side of the relationship is "fixed up" to display information for the appropriate customer.



    / 544