Sunteți pe pagina 1din 71

Microsoft Office

Access 2013
Queries

11
Objectives
 Maintain a database
 Understand action queries
 Create an Update query
 Create an Append query
 Use a make table query
 Create a delete query
 Create find unmatched queries
 Understand find duplicate queries
2
Queries
What is a query?
 A query is actually the name for any
database manipulation operation.
 The most commonly used type is a select
query, which is a way of selecting what
fields in what records are to be displayed.
Creating a query
 You can create only very simple queries
by using the Query Wizard.
 We’ll create all Queries using the Design
View for queries.
 Once designed, the results of a query can
be displayed in Datasheet View.
Select queries
 A select query is the most common type of
query.
 It retrieves data from one or more tables and
displays the results in a datasheet where
you can update the records (with some
restrictions).
 You can also use a select query to group
records and calculate sums, counts,
averages, and other types of totals.
ACTION QUERIES
Action Queries
 Change underlying table data
 Select queried do not
 Can be used to create new tables
 Cannot be undone
 Play a large role in maintaining a database

8
Types of queries you can create
in Microsoft Access
 Select queries
 Crosstab queries (actually a special
kind of select query)
 Action queries
 Make-table queries
 Delete queries
 Update queries
 Append queries
 In addition, any of these types can be
designed to prompt the user for
information, in which case it is called a
parameter query.
i. Update Queries
 Changes value(s) in one/more records
 Use the Update query button
 Database Tools tab, Query Type group

Update button

11
Creating an Update query
 Identify record source
 Show desired tables
in design grid Update
value
 Switch from Select to
Update Query
 Insert Field to be
updated
 Enter the value to be
updated
 Value entered in
Update To field
12
Testing Action Queries
Check Datasheet
View first

 Test all action queries before execution


 Choose Datasheet View
 Allows preview of results before execution
13
Running an Update Query

Before Update query

After Update query


Run button

 Click the Run button


 Design tab, Results group
 View table to see the results
14
Specifying Criteria in an Action
Query

Criteria

 Specified in the field being update


 Also specified using an additional field(s)

15
Using Criteria with an Update
Query Before update query with criteria

Criteria

After update query with


criteria

 Specifies specific records for update


 Works the same as in a select query
16
ii. Append Queries
 Adds records from one table to another
 Data types between tables must match
 Validation rules in existing table apply
 Records not added if validation rule violated
 Source table fields may be ignored
 Do not add to design grid
 If auto-number fields exists in target table
 Do not import auto-number fields

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 17


17
Creating an Append Query
Append button

Append dialog box

 Identify target and source tables


 Click Append button
 Choose Target table from Append box
 May also browse to choose another database
18
Creating an Append Query

Fields to append
added to design grid

 Add fields from Source table to design grid


 Test in Datasheet view
 Run query
19
Append Query Results
February Agents after
append

February Agents
before append

September Agents
before and after
append

 Data is appended to existing table

20
iii. Make Table Query

 Almost the same as append query


 Table must preexist in append query
 No pre-existing table in make table
query
 Target database must exist

21
Creating a Make Table Query
Make Table
button

Make Table dialog


box

 Identify the source table


 Click Make Table button
 Enter table name or choose database
 Accomplished in the Make table dialog box
22
Running a Make Table Query
Fields added in
design grid

Test in Datasheet
view

 Add fields to design grid


 Test in Datasheet view
 Run query
23
Make Table Query Results
Source Table

Results of make
table query

 New table added to Objects pane


 Open table to view results

24
iv. Delete Queries
 Can delete all records from table
 Criteria used to delete specific records
 If no criteria, all records deleted

 Very powerful – cannot be undone


 Setup is same as other action queries

25
Creating a Delete Query
Add table to Delete button
design grid

 Add table to Design grid


Delete Query with
 Click Delete Button criteria
 Add field(s) to design grid
 Use criteria to specify delete conditions

26
Running a Delete Query
Test in Design
view
Table before
delete query

Table after
delete query

 Test query in Datasheet view


 Run Query
 Open table to view results
27
27
Hiding a Query

All queries Some queries


shown hidden

 Prevents accidental execution of action queries


 Right-click query
 Click the Hidden checkbox
28
About Unmatched Queries
Unmatched
Query
results

 Compares records in two related tables


 Returns records found in only one table
 Query Wizard easiest method of creation
29
Unmatched Query Wizard

Find Unmatched
Query

 Run Query Wizard


 Select Find Unmatched Query Wizard

30
Unmatched Query Wizard
Select first table source

 Select first table


source
 Select second table
source

Select second table source

31
Unmatched Query Wizard
Select duplicate fields

 Select the duplicate


fields
 Click the <=> button
 Select display fields for
query results
 Click Finish
Select fields for query
results

32
About Find Duplicates Query
Query
Wizard

Find
Duplicates
query result
 Finds duplicate data
 Aids in data entry error detection
 Allows administrative use of purposely duplicated data
 Easily created with the Query Wizard
33
CRITERIA
Expressions

Words begin or end with a: LIKE a* or LIKE *a


Find between values: (NOT) BETWEEN 45000 AND 78000
Find exact match value: 1/10/2005
Arithmetic operators
 ^ (exponentiation)
 * (multiplication)
 / (division)
 \ (division that returns an integer result)
 Mod (divide two numbers and return only the
remainder)
 + (addition)
 - (subtraction)
‘Like’ and ‘Is’ comparison
operators
 The Like operator is a pattern-matching
operator used to compare two strings.
 With Like, the single-character wild-card ‘?’
and the arbitrary-length wild-card ‘*’ can be
used, as in ‘Like A*’.
 The Is operator is most often used in the
context Is Null, to test whether a field is
blank. (Is Not Null is satisfied in just the
opposite case.)
Logical operators
 And  Used to perform a logical
conjunction on two
expressions.
 Used to perform a logical
 Or
disjunction on two
expressions.
 Used to perform a logical
 Not negation on an expression.
Criteria
Criteria: Between
Criteria: Wildcards
Expressions
Expressions: Dates & Nulls
Find an Exact Match
 Barbara would like to have a list of all orders billed on
01/15/2001.
 The list must include following fields; OrderNum, Paid,
InvoiceAmt, BillingDate, CustomerName, State,
OwnerName, and Phone
 Save as Jan Orders
Using Like a*
 Barbara wants to know a list of all Customer Names that
begins with M. The list must include following fields;
Customer Name, Order Number, First Contact and
Billing Date. In addition, the Order Number must be
sorted in ascending order.
 Save as Customer Name Begins With M
Using Between ** and **
 Barbara wants to know a list of all Product Code
between 2465 and 2763. The list must include following
fields; Product Code, Coffee Name and Price.
 There should a space between number and word
 Save as Product Code Between 2465 And 2763.
Using Greater than (>)
 Barbara wants to know those records whose InvoiceAmt
exceeds $2,000.
 Use the Customer Orders query.
 The query must include only following fields; Customer
Name, Invoice Amount, and Billing Date.
 Save as High Invoice Amounts
Using And and Or Operators

The And
condition.

The Or
condition.
Creating 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.
Using AND
 Leonard asks Barbara for a list of orders billed on
01/15/2001 that are still unpaid. He wants to know which
customers are slow in paying their invoices.
 Use the Customer Orders query again and make sure
that this query includes following fields; CustomerName,
Paid, BillingDate, and InvoiceAmt.
 01/15/2001 AND unpaid (No)
 Save as Unpaid Jan Orders
Using OR

 Leonard wants to determine which restaurant


customers are most valuable to Valle Coffee.
Specifically, he wants to see a list of those customers
who have been placing orders for many years
(specifically, first contact date - earlier than
01/01/1994: <=01/01/1994 ) OR who place orders for
a substantial amount of money (greater than $2,000:
>2000), so that he can call the customers personally
and thank them for their business.
Using OR
 Make sure that this query includes following fields;
CustomerName, InvoiceAmt, and First Contact.
 He also wants to have a sorted output in ascending
order based on the customer name.
 If you want, you can use the Customer Orders query
again
 Save as Top Customers
A 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.
Use of “Build” Function
Performing Calculation

 Leonard is considering adding a 2% late charge to


the unpaid invoices. He only wants to include
following fields; Customer Name, Paid, Billing Date,
and Invoice Amount.
 Set unpaid field to “No”
Aggregate Functions
Aggregates: Criteria

Before After
Aggregates: Where

Oh, Microsoft!?
PARAMETER
Parameter queries
 A parameter query prompts for criteria
each time it's run.
 A parameter query displays one or more
predefined dialog boxes that prompt you
for the parameter value (criteria).
 You can also create a custom dialog box
that prompts for the query's parameters.
 For example, suppose we often want to see
certain data for all customers living in a
particular city, where that city may be
Miami, or Coral Gables, or any one of many
other cities.
 Rather than create a separate query with
the city built in, it may make more sense to
create a single parameter query that
prompts for the city before it runs.
Creating a parameter query
 In the Criteria cell for each field you want
to use as a parameter, type a prompt
enclosed in square brackets([]). Microsoft
Access will display this prompt when the
query is run. The text of the prompt must
be different from the field name, although
it can include the field name.
 For example, a simple modification of our
earlier query to list all customer data for
Miami customers makes it a parameter
query that prompts for the city:
 For a field that displays dates, you can
display the two prompts "Type the
beginning date:" and "Type the ending
date:" to specify a range of values. In the
field's Criteria cell, type Between [Type the
beginning date:] And [Type the ending
date:].
CROSSTABLE
Crosstab queries
 A crosstab query displays summarized
values (sums, counts, and averages) from
one field in a table and groups them by
one set of facts listed down the left side of
the datasheet and another set of facts
listed across the top of the datasheet.
To create a crosstab query
using a wizard
 In the Database window, click the Queries
tab, and then click New.
 In the New Query dialog box, click
Crosstab Query Wizard.
 Click OK.
 Follow the directions in the wizard dialog
boxes. In the last dialog box, you can
choose to run the query or see the query's
structure in Design view.

S-ar putea să vă placă și