xp new perspectives on microsoft access 2002 tutorial 31 microsoft access 2002 tutorial 3 –...

35
New Perspectives on Microsoft Access 2002 Tutorial 3 1 XP Microsoft Access 2002 Tutorial 3 – Querying a Database

Upload: phebe-barton

Post on 18-Jan-2016

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 1

XP

Microsoft Access 2002

Tutorial 3 – Querying a Database

Page 2: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 2

XPLearn how to use the Query window in Design view

• The Query window in Design view allows you to specify the results you want for a query.

• In the query Design view, you can specify which fields you want to be included in your query results.

• You can also control what records are displayed in the query by specifying select query criteria.

• Each column in the design grid represents a field that will be used in the query.

• You can run the query at anytime to view the results according to the current specifications.

Page 3: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 3

XPAdd fields to a query in Design view

• The design grid is used to specify the fields and records you want to see.

• Add fields to the design grid by double-clicking the field in the field list or by dragging the field to the design grid. – Remember that the results of a query provide a temporary view of

the data– The contents displayed in a query are a result of the fields you

select and the criteria you specify in the query design grid• When you use the query design window, you use Query

By Example (QBE). When you use QBE, you give Access an example of the information you wish to see when you run your query.

Page 4: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 4

XPThe Query Design view window

Fields used in the query are selected from the field list and added to the design grid.

You can run the query at any time by clicking the Run button.

Page 5: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 5

XPCreate, run, and save queries

• From the fields list, either drag or double-click fields that you want included in the query. – A query result differs from a table's datasheet view in that only

selected fields are displayed • If you move a field to the design grid and then you want to

remove it, you can click on the field's column and then press the delete key.

• The results of the query will be displayed in order by the primary key of the table unless you specify another sort order.

• You can save the query and give it a name by pressing the save button on the toolbar.

Page 6: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 6

XPSelecting, displaying and sorting fields

You can use the mouse to click and drag fields to the design grid.

Fields can be added to the query but not displayed in the results sheet by removing the check mark from the Show: field.

You can specify fields other than the primary key to use as sort fields.

Page 7: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 7

XPA sample query datasheet

A query datasheet looks similar to a table datasheet, but shows only selected fields.

A query datasheet window also has navigation bars similar to other database windows.

Page 8: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 8

XPUpdate data using a query

• You can use the query datasheet to update data in a table.

• It is important to realize that the query datasheet is a temporary view of the data. However, when you update data by means of the query datasheet, the updates are placed directly into the underlying table.

• You can only update fields that are represented in the query.

• To observe the actual changes made to the table, close the query and open the table in datasheet view.

Page 9: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 9

XPAccess is a relational database

• A database is almost always a collection of tables. Access is a relational database management system that allows you to form relationships between the tables.

• When you form a relationship between tables, you are joining the tables.

• Tables are joined on common fields between the tables.

• When tables are joined, you can view data from both tables as if the tables were one combined table.

Page 10: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 10

XPA one-to-many relationship

• Tables can be joined in three ways; one-to-one, one-to-many, and many-to-many.

• A one-to-many relationship exists when one table has many records associated with a given value but the related table has only one record for that value.– The table with one record is called the primary table

– The table with many records is called the related table

• Queries can be defined to use the relationship to extract data from both tables in a single query.

Page 11: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 11

XPAn example of a one-to-many relationship

The Employer table is related to the Position table via the common field EmployerID.

The Employer table has one record for EmployerID value 10126. The Position table has two records.

Page 12: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 12

XPQuery results for a one-to-many relationship

Queries can be built and saved that extract data from both the primary table (Employer) and the related table (Position).

Page 13: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 13

XPUsing referential integrity

• When dealing with related tables, you need to decide if you want to enforce referential integrity.

• Referential integrity allows you to maintain the integrity between related tables.

• The rules associated with referential integrity specify that when you add a record to a related table, there must be a matching record in the primary table.

• If you choose to enforce referential integrity, you can insure that you will not have orphaned records (records that have no matching record in the primary table).

Page 14: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 14

XPUse cascaded updates and cascaded deletes

• In addition to referential integrity, you can also tell Access to implement cascaded updates and cascaded deletes.

• If you choose cascaded updates, making a change in a field that is common to two related tables will cause the update to be made in both tables.

• Cascade deletes is similar. If you delete a field that is common to two tables, the deletion will take place in both tables.

• You should carefully consider whether you want to implement these features, as they can have dramatic effects on your data.

Page 15: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 15

XPSelecting the tables for a relationship

When all tables are added, click the Close button.

To define a relationship, open the Show Table dialog box by clicking the Relationship button on the toolbar.

Select each table you want to be in the relationship and click the Add button.

Page 16: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 16

XPSetting relationship options

The Edit Relationships dialog box is where you can determine the type of relationship, and set referential integrity and cascade update/delete options.

Page 17: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 17

XPThe Relationships window

You can see the tables, fields, and relationship types for any relationship in the Relationship window.

The lines indicate the common fields involved in each relationship.

The symbols indicate the type of relationship.

The Employer table has two one-to-many relationships--one with the Positions table, and one with the NAICS table.

Page 18: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 18

XPSort data in a query

• You can control the order of data displayed in a query by altering the queries sort key.

• Choose a field on which you want the data to be sorted and then specify whether you want the data sorted in ascending or descending order.

• Specify multiple fields as the sort key if you want to arrange data in sorted order within groups of data.

• Specify sort criteria for the query in the query design grid.

Page 19: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 19

XPSort results by data type

Depending on the type of data that you specify as a sort field, the data will be sorted according to the data type as shown in the following figure.

Page 20: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 20

XPSort single or multiple fields

To sort only one field, click in the desired field to select it, and then click the Sort Ascending or Sort Descending button on the toolbar. To sort multiple fields, you must specify them in the query design grid.

Page 21: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 21

XPFilter data in a query

• Sometimes you may want to isolate a certain portion of data in your query results.

• You can set a filter, or a set of restrictions, to specify what records you want to view in the results. Only those records that match the criteria will display in the results datasheet.

• There are two methods used to make your selections: Filter By Selection or Filter By Form.

• If you no longer want to view the data with the filter applied you can remove the filter.

Page 22: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 22

XPUsing Filter By Selection

Select the value you want to use as a filter, then click the Filter By Selection button.

Click the Remove Filter button to display all records.

Access displays a message to tell you a filter is applied.

Page 23: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 23

XPSpecify an exact match condition in a query

• You can specify an exact match condition, which allows you to specify that only records that match your criteria will be displayed.

• To specify an exact match, use the criteria row in your query design grid.

• When you run the query after specifying an exact match, only those records that meet the criteria will be displayed.

• An exact match condition specifies that the value in the specified field must match the condition exactly.

Page 24: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 24

XPExact match operators and results

When creating an exact match condition you can use any of the comparison operators shown in the figure to the right.

Page 25: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 25

XPChange a datasheet's appearance

• You can make several alterations to the way a query datasheet is displayed.

• You can change the font type, the font size, and the size of the columns.

• To alter the font of the datasheet:– Click Format on the menu bar– Select Font from the dropdown menu– In the Font dialog box, select the font you want and also change

the size of the font

• Columns can be resized individually or you can select and resize all of them at one time.

Page 26: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 26

XPUse a comparison operator to match a range of values

You can use a comparison operator, such as the greater-than sign (>), to specify that you want to see only records that have a value greater then the value specified. This design grid shows a datasheet view with a criteria set to show records that have a wage value greater than or equal to 17.

Note that this design grid also shows that the StartDate field had criteria supplied from another run of the query. This condition needs to be deleted, or otherwise, both conditions would be applied to this query.

Page 27: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 27

XPUse the And and Or logical operators

• When you need to use multiple conditions for a query, you can use the logical operators to combine conditions. – The And logical operator specifies that both conditions must be

met

– The Or logical operator specifies that one or the other of the conditions must be met

• When you enter two conditions on the same row of the query design grid, an And condition is created.

• If you enter two conditions that are on separate rows, an Or condition is created.

Page 28: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 28

XPIllustration of And and Or logic

The And condition.

The Or condition.

Page 29: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 29

XPCreating And and Or conditions in the design grid

And conditions must be specified on the same line.

Or conditions must be specified on different lines.

Page 30: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 30

XPUse multiple undo and redo

• In Access 2002, you can Undo and Redo multiple actions at one time.

• While working in design view for tables, queries, forms, and reports, you can use the undo recent actions button.

• The Undo button on the design toolbar has an associated list box. You can open the list box and then select the action you want to undo.

• If you undo an action and then change your mind, you can also redo actions by the same method, using the Redo button.

Page 31: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 31

XPUsing the Undo actions button

The figure below shows the list of actions associated with the Undo button. Notice that there are two actions in the list. The list would change according to the most recent actions you have taken in your query.

If you click on any action other than the first one in the list, Access will undo all the actions above and include the one selected from the list.

Page 32: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 32

XPPerform calculations in a query using calculated fields and expressions

• Queries can be built to perform a calculation as part of the query.

• Expressions can be entered into the query design grid. • Make certain that you are following the rules of

precedence. • To perform a calculation in a query, you must add a

calculated field to the query design. • You have three options for entering expressions:

– Enter the expression directly into the field text box– Enter the expression in the Zoom box– Enter the expression in the Expression Builder

Page 33: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 33

XPThe Expression Builder dialog box

The Expression Builder allows you to create expressions by clicking fields and operators from list boxes and buttons.

Click a button to choose an operator.

Choose an Access object from this pane.

Choose a field for the selected object from this pane.

Page 34: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 34

XPA calculated field in the query datasheet

Expression Builder adds your calculated field to the query design grid. You can then assign it a name, which will display in query datasheet view when the query is run.

Page 35: XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database

New Perspectives on Microsoft Access 2002 Tutorial 3 35

XPUsing Aggregate Functions

Access has several Aggregate Functions that can be used to calculate various statistical information.

Aggregate functions are specified in the Total row of the design grid. They can be assigned by clicking the Totals button on the Query Design toolbar.