0% found this document useful (0 votes)
66 views21 pages

COMP1115 Week 11 Microsoft Access Relationships and Queries Part 1 W19

This document provides a tutorial on querying and filtering data in Microsoft Access. It explains that queries allow users to display, sort, and update data from one or multiple tables based on specified criteria. The tutorial demonstrates how to create queries using the Query by Example tool, set criteria to filter records, sort results, and define relationships between tables to perform multi-table queries. It also covers using operators like AND and OR to combine multiple selection criteria.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views21 pages

COMP1115 Week 11 Microsoft Access Relationships and Queries Part 1 W19

This document provides a tutorial on querying and filtering data in Microsoft Access. It explains that queries allow users to display, sort, and update data from one or multiple tables based on specified criteria. The tutorial demonstrates how to create queries using the Query by Example tool, set criteria to filter records, sort results, and define relationships between tables to perform multi-table queries. It also covers using operators like AND and OR to combine multiple selection criteria.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Microsoft Access – Tutorial 3

Querying a Database
 a query is a question you ask about data stored in a

database
 you tell Access what fields you need and what criteria

Access should use to select the records


ex. find records for employers located in a specific State or
Province
 queries allow you to:

 display selected fields and records from a table


 sort records
 perform calculations
 generate data for forms, reports and other queries
 update data in the tables in a database
 find and display data from two or more tables
Microsoft Access – Tutorial 3
Query by Example (QBE)
 You Query by Example by giving Access an example of the

information we are requesting


 Access then retrieves the information we are requesting

 We want to create a query to display:

 employer ID
 employer name
 city
 contact first name
 contact last name
 web site information
 for each record in the Employer table
Microsoft Access – Tutorial 3
Query by Example (QBE)
 open the Northeast database we created last week

(I have included a copy in the Common Directory on the


desktop)
 Click Create > Query Design

 show the Employer Table and the close the Show Table

dialog box
 maximize the Query window
Microsoft Access – Tutorial 3
Query by Example (QBE)
 you will see the field list in a box entitled Employer (the

table name)
 you can double-click or drag fields to the design grid

 in the design grid, you include the fields and record selection

criteria for the information that you want to see


 you can see the current results of your query at any time by

View button or the Run button on the top left


 the current results will appear in a datasheet view
Microsoft Access – Tutorial 3
Query by Example (QBE)
 drag the split bar and resize the windows as you like

 double-click the following fields in order: EmployerID,

EmpoyerName, City, ContactFirstName, ContactLastName


and Website
 note the checkbox that that allows you to show or hide fields

 click Run (or View (Datasheet View)) to see the results in a

datasheet
 click the View button to return to Design View

 click the Save button on the toolbar and name the query

EmployerAnalysis
Microsoft Access – Tutorial 3
Updating Data using a Query
 return to the Datasheet view

 indicate that The Berkshire House (10223) now has a

website
 change the Contact person for Alpine Touring Center

(10152) to Takeshi Yamamoto


 close the Query and note that it has been added to the

Queries in the Objects bar


 open the Employers table and verify that the changes have

been made
 close the Employers table
Microsoft Access – Tutorial 3
Table Relationships
 remember that Access is a relational database management

system
 relationships are created among tables by using common

fields
 this process is often called a join

 when you join tables with a common field, you can extract

data from them as if they were one table


 the Employer and Position tables will be joined with the

common field EmployerID (a foreign key)


 you can use a query form or report to extract selected data

from each table even though the information is in two tables


Microsoft Access – Tutorial 3
One-to-Many Relationships
 a one-to-many relationship exists between two tables when

one record in the first table matches zero, one or many


records in the related table
 and when one record in the in the second table matches

exactly one record in the first table


 the primary table is the one table in the one-to-many

relationship
 this is the Employer table
 the related table is the many table
 this is the Position table
Microsoft Access – Tutorial 3
One-to-Many Relationships
 orphaned records can be created when inconsistencies

occur between tables


 for example:

 if an Employer ID is changed in the Employer table


 if an Employer is deleted from the Employer table
 if a position is added for an Employer that doesn’t exist in the
Employer table
Microsoft Access – Tutorial 3
One-to-Many Relationships
 Referential Integrity is a set of rules that Access uses to
prevent records from being orphaned
 it maintains consistency between tables when you update
data
 when you add a record to a related table, a matching record
must already exist in the primary table
 if you attempt to change the value of a primary key in the
primary table, Access prevents the change if matching records
exist in a related table
(however, if the cascade updates option is chosen, the
foreign key values will be updated automatically)
 Access prevents the deletion of records in a primary table if
matching records exist in a related table
(if cascade deletes option is selected all records with that key
will be deleted – this is not recommended)
Microsoft Access – Tutorial 3
Defining Relationships
 you can define relationships between tables using the

Relationships window
 Database Tools > Relationships

 if asked, add Employer and Position and close the Show

Table dialog box


 lengthen and widen each window to show all fields

 click and drag EmployerID from the Employer Table to

EmployerID in the Position table


 the Edit Relationships window will appear

 the Primary Table, Related Table and Common Field will be

visible
Microsoft Access – Tutorial 3
Defining Relationships
 check Enforce Referential Integrity

 note that the two Cascade options become available

 check Cascade Update Related Fields

(remember that it is not recommended to check the Cascade


Delete Related Records as you may inadvertently delete
records)
 click create and note the join line that appears and the

symbols on it

 now create a one-to-many relationship between the NAICS


table and Employer Table with NAICS code as the common
field (foreign key)
 click the Save button and Close the window
Microsoft Access – Tutorial 3
Multi-Table Queries
 now that we have created Relationships among (joined) our
tables, we can query them as one
 create a new query in design view
 add the Employer and Position tables
 create a query in the following order:
 EmployerName
 City
 StateProv
 Openings
 PositionTitle
 StartDate
 EndDate
 run it and save the query as EmployerPositions
Microsoft Access – Tutorial 3
Sorting Data in a Query
 sorting is rearranging records in a specified order or
sequence
 open the EmployerPositions query in datasheet view
 select the first record in the Employer Name field and use
the sort buttons (drop-down arrows on column heading) to
sort alphabetically
 to sort by more than one field go to Design View
 select the entire column for Openings
 click and drag it so that it is the last field (after EndDate)
 use the Sort field to sort PositionTitle Ascending
 sort Openings Descending
 run the query
 within groups of fields with the same Position Title, the
record sorts the number of openings from highest to lowest
 save the query
Microsoft Access – Tutorial 3
Filtering Data in a Query
 you can filter query results using the Filter by Selection

button
 double-click the word clerk in one of the Position Title

records
 click the Filter by Selection button (located in Home > Sort

and Filter group > selection)


 you will see only results for different kinds of clerks

 click the toggle filter button to turn off the filter

 filter results for New Hampshire (NH)

 Untoggle the filter and close the query


Microsoft Access – Tutorial 3
Defining Record Selection Criteria
 you can specify a condition which is a criteria or rule that

tells Access which records to display


 we will Create > Query Wizard using a condition

 click Simple Query Wizard and make sure the Table:

Position is selected
 add all the fields by clicking >>

 remove both PositionID and EmployerID from the Selected

Fields list box


 to add fields from another table after the current set of fields,

select Openings
 now add EmployerName, StateProv, ContactFirstName,

ContactLastName and Phone from the Employer Table


Microsoft Access – Tutorial 3
Defining Record Selection Criteria
 click the Next button

 make sure the Detail radio button is selected and click Next

again
 name the Query HousekeepingPositions and click the Modify

the Query Design radio button and click Finish


 in the PositionTitle Criteria type housekeeping

 Access adds “ signs before and after the criteria

 run and save the query

 only housekeeping positions will be displayed


Microsoft Access – Tutorial 3
Formatting the Appearance of the sheet
 select all the records

 Home > Font Size (in the Text Formatting group)

 change the size of the fonts to 8

 double click between columns to resize them


Microsoft Access – Tutorial 3
Setting Range Criteria
 open the HousekeepingPositions query in Design View

 remove the PositionTitle criteria

 set the Wage criteria >=17

 run the query and Save (File > Save Object As) it as

HighWageAmounts
Microsoft Access – Tutorial 3
Using Multiple Selection Criteria with the And operator
 open the HousekeepingPositions query in Design View

 add “MA” as criteria for StateProv

 run the Query and you’ll see results that match both criteria

 save the query as HousekeepingPositionsInMaine


Microsoft Access – Tutorial 3
Using Multiple Selection Criteria with the Or operator
 create a new query in Design view

 use the Employer and Position tables and add the following

fields: EmployerName, City, PositionTitle, HoursPerWeek,


and Experience
 set the HoursPerWeek criteria to <30

 in order to select records in which either (not both) of the

conditions is met type Yes in the or: field


 run the query

 sort it alphabetically ascending by Employer Name

 Save it as HoursorExperience

You might also like