Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

lab5

Practical

Uploaded by

humairaashraf486
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

lab5

Practical

Uploaded by

humairaashraf486
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

MS Access Lab 5

Topic: Query

Summary

• Introduction to Query
• Creating Select Queries
• Creating Queries of Multiple Tables
• Creating Calculation Queries

Introduction to Query
You use queries to view, change, and analyze data in different ways. You can also use them as the
source of records for forms and reports.

1
The Select Query
The most common type of query is a select query. A select query retrieves data from one or more tables
using criteria you specify, and then displays it in the order you want.

Creating a Query

Creating a Select Query with a Wizard


The Simple Select Query Wizard creates queries that retrieve data from the fields you specify in one or
more tables or queries. If you want, the wizard can also sum, count, and average values for groups of
records or all records, and it can calculate the minimum or maximum value in a field. You can't limit the
records it retrieves by setting criteria, however.

2
1. In the Database window, Go to Objects à Queries à Create Query by using Wizard
2. In the Simple Query Wizard window (Fig.1), click the name of the table or query you want to base
your query on, and then select the fields whose data you want to retrieve.
3. Click an additional table or query if desired, and then select the fields you want to use from it. Repeat
this step until you have all the fields you need.
4. Follow the directions in the wizard dialog boxes. In the last dialog box, you can choose either to run
the query or to see the query's structure in Design View1.

Note:If the resulting query isn't exactly what you want, you can rerun the wizard or change the query in
Design view.

1
Design view -Queries
A window in which you design queries.

To open a query in Design view, go to the Database window, click on Objects à Queries, click the query
you want to open, and then click Design.
If the query is already open, you can switch to Design view by clicking View on the toolbar.

3
Fig. 1 : Simple Query Wizard window

Creating a Select Query with Design View


1. In the Database window, Click on Object à Queries à Create Query in Design View
2. In the Show Table dialog box, click the tab that lists the objects whose data you want to work
with.
3. Double-click the name of each object you want to add to the query, and then click Close.
4. If you have multiple tables or queries in the query, make sure they are connected to each other
with a join line so that Microsoft Access knows how the information is related.
5. Add fields to the query by dragging the field names from the field list to the design grid.
6. Refine your query by entering criteria, adding a sort order, creating calculated fields, computing
the sum, average, count, or another type of total on the data it retrieves, or otherwise modifying
the query's design.
7. To save the query, click Save on the toolbar. Enter a name that follows Microsoft Access
object-naming rules, and then click OK.
8. To see the results of the query, click View on the toolbar.

Ways to bring together data from multiple tables/queries in a query


The power of queries lies in being able to bring together or perform an action on data from more than one
table or query.

Example: You might want to view a customer's information with the orders the customer placed. To see
this information, you need data from the Customers and Orders tables. When you add more
than one table or query to a query, you need to make sure their field lists are joined to each
other with a join line so that Microsoft Access knows how to connect the information.

4
Note: If tables in a query aren't joined to one another, either directly or indirectly, Microsoft Access doesn't
know which records are associated with which, so it displays every combination of records (called a
"cross-product" or "Cartesian product") between the two tables. Therefore, if each table had 100
records in it, the query's results will contain 10,000 records (100X100)! It also means the query
might take a long time to run and ultimately might produce less meaningful results.

If you previously created relationships between tables in the Relationships window, Microsoft Access
automatically displays join lines when you add related tables in query Design view. If referential
integrity2 is enforced, Microsoft Access also displays a "1" above the join line to show which table is on
the "one" side of a one-to-many relationship and an infinity symbol to show which table is on the
"many" side.

Even if you haven't created relationships, Microsoft Access automatically creates a join if you add two
tables to a query and the tables each have a field with the same or compatible data type and if one of the
join fields is a primary key. The "one" and "many" symbols are not displayed in this case, because
referential integrity is not enforced.

Sometimes the tables you add to the query don't include any fields that can be joined. In this situation,
you have to add one or more extra tables or queries to serve solely as a bridge between the tables whose
data you want to use.

Example: If you add the Customers and Order Details tables to a query, they won't have a join line
between them because they don't have any fields that can be joined. But the Orders table is
related to both tables, so you can include the Orders table in your query to provide a
connection between the other two.

2
referential integrity:
Rules that you follow to preserve the defined relationships between tables when you enter or delete
records. If you enforce referential integrity, Microsoft Access prevents you from adding records to a
related table when there is no associated record in the primary table, changing values in the primary table
that would result in orphan records in a related table, and deleting records from the primary table when
there are matching related records in a related table.

5
Once tables and queries are joined, and you've added fields from both tables/queries to the design grid3
in query Design view, the default join tells the query to check for matching values in the join fields. (This is
called an inner join in database terminology.) When it finds matches, it combines those two records and
displays them as one record in the query's results. If one table or query doesn't have a matching record in
the other table or query, neither record appears in the query's results.

Fig 2: Sample of Query in Northwind.mdb

3
design grid
The grid that you use to design a query or filter in query Design view or in the Advanced Filter/Sort
window. For queries, this grid was formerly known as the QBE grid.

6
Creating a Calculation Performing Query
There are many types of calculations you can perform in a query. For example, you can calculate the sum
or average of the values in one field, multiply the values in two fields, or calculate the date three months
from the current date.

When you display the results of a calculation in a field, the results aren't actually stored in the underlying
table. Instead, Microsoft Access reruns the calculation each time you run the query so that the results are
always based on the most current data in the database. Therefore, you can't manually update the
calculated results.

To display the results of a calculation in a field, you can use a predefined calculation that Microsoft
Access provides or custom calculations you define. Use the predefined calculations, called "totals" if you
want to compute the following amounts for groups of records or for all the records combined: sum,
average, count, minimum, maximum, standard deviation, or variance. You choose one totals calculation
for each field you want to calculate.

You can calculate some types of totals using the Simple Query Wizard. Or, you can calculate all types of
totals using the Total row in the query design grid, where you select the aggregate function for the
calculation you want to perform on a field.

In the query design grid, you can also specify criteria to limit the groups for which totals are calculated,
limit the records included in the calculation, or limit the results that are displayed after the calculation is
performed.

A custom calculation enables you to perform numeric, date, and text calculations on each record using
data from one or more fields. For example, with a custom calculation, you can multiply one field's values
by a set amount, find the difference between two dates stored in separate fields, combine several values
in a Text field, or create sub-queries. Using the options in the Total row in the design grid, you can
perform the calculation on groups of records and calculate a sum, average, count, or other type of total on
the calculated field. To use total option, click Totals on the tool bar.

For custom calculations, you need to create a new calculated field directly in the design grid. You create a
calculated field by entering an expression into an empty Field cell in the query design grid.

The expression can be made up of multiple calculations for example,


Sum([UnitsInStock]+[UnitsOnOrder]) . You can also specify criteria for a calculated field to affect the
results of the calculation.

7
You don't have to display the results of a calculation in a field, however. Instead, you can use them as
criteria to determine the records the query selects or to determine which records to perform an action on.

For example, you can specify the following expression in the Criteria row to tell the query to return only
records that have values in the RequiredDate field that are between today's date and three months from
today's date.

You can also use a calculation to update data from an update query. For example, you can enter the
following expression in the Update To cell to increase all the values in the UnitPrice field by 5 percent.

Calculate a sum, average, count, or other total on groups of records in a query


1. Create a select query in Design view. Add the tables whose records you want to use in the
calculation, and then add the fields on which you want to perform calculations, define groupings, and
specify criteria.
2. Click Totals on the toolbar.

Microsoft Access displays the Total row in the design grid.

3. For the field or fields you want to group on, leave Group By in the Total cell.

4. For each field you want to calculate, click its cell in the Total row, and then click one of the following
aggregate functions: Sum, Avg, Min, Max, Count, StDev, or Var.

5. If you want, enter criteria to affect the results of the calculation.

6. If you want, sort the results.

7. Click View on the toolbar to view the results.

8
About aggregate functions and other options in the query design grid's Total row

Select To find the Use with these field data types


Sum Total of the values in a field. Number, Date/Time, Currency,
and AutoNumber
Avg Average of the values in a field. Number, Date/Time, Currency,
and AutoNumber
Min Lowest value in a field. Text, Number, Date/Time,
Currency, and AutoNumber
Max Highest value in a field. Text, Number, Date/Time,
Currency, and AutoNumber
Count Number of values in a field, not Text, Memo, Number, Date/Time,
counting Null (blank) values. Currency, AutoNumber, Yes/No,
and OLE Object
StDev Standard deviation of the values Number, Date/Time, Currency,
in a field. and AutoNumber
Var Variance of the values in a field. Number, Date/Time, Currency,
and AutoNumber

Writing Queries in SQL view:


MS Access allows you to write queried directly into SQL statements. When you are in the design view of
a query, click view and select SQL view. Here you can directly type SQL statements. There may be a
couple of reasons why you would want to do so. You may be very proficient with SQL, in which case the
design view is not necessary. And sometimes it is more convenient to use design view to create the
basic part of a query and then go to SQL view and manipulate it there to come up with the required query.

You might also like