Query Basics

69
Query Basics

Transcript of Query Basics

Query Basics

ISM 322 – Database Theory and Applications © sfreires

OBJECTIVES

You will know your way around the Design view of the Queries object

You will also be able to know how to

Write selection criteria with or without using the Expression Builder

Write total and crosstab queries

ISM 322 – Database Theory and Applications © sfreires

OUTLINE

1. Queries View

Design View

Datasheet View

2. Select Queries

Selecting Data from a Single Table

Searching Multiple Tables

Using a Query Wizard

ISM 322 – Database Theory and Applications © sfreires

OUTLINE

3. Action Queries

Updating Groups of Rows

Creating a New Table

Inserting Data From Another Table

Deleting Groups of Rows

Queries View

ISM 322 – Database Theory and Applications © sfreiresQU

ER

IES

VIE

W

ISM 322 – Database Theory and Applications © sfreires

QUERIES VIEW

Query

Opens a Query window in Datasheet view

Opens a Query window in Design view

ISM 322 – Database Theory and Applications © sfreires

QUERIES VIEW

The Database window also contains two special icons that provide shortcuts for creating a new query

ISM 322 – Database Theory and Applications © sfreires

Design View

To open an existing query in Design view, click the Queries button in the Database window

Select the query you want, and click the Design button

The upper part of the Query window contains field lists, and the lower part contains the design grid

ISM 322 – Database Theory and Applications © sfreires

Design View

ISM 322 – Database Theory and Applications © sfreires

Datasheet View

Click the Query View button on the toolbar to run the query and see the query results in Datasheet view

Run Button

ISM 322 – Database Theory and Applications © sfreires

Datasheet View

Select Queries

ISM 322 – Database Theory and Applications © sfreires

SELECT QUERIES

When you define and run a select query, Microsoft Access creates a recordset of the selected data

You can work with a recordset in the same way that you work with a table

But unlike a real table, a recordset doesn't actually exist in your database

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

One advantage of using queries is that they allow you to find data easily in multiple related tables

Queries are also useful, however, for sifting through the data in a single table

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

The easiest way to start building a query is to open the Database window

Select the table you want

Select Query from the New Object toolbar button's drop-down list

DEMO

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table The first step in building a query is to

select the fields you want in the recordset Drag or double-click on from one of the

field lists in the upper part of the window

Select the field from the drop-down list

The asterisk (*) is the shorthand for "all fields in the table or the query"

DEMO

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

If you want to test for any of several values, enter the values in the Criteria row, separated by the word Or

You can also test for any of several values by entering each value in a separate Criteria or Or row for the field you want to test

DEMO

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

When you enter criteria for several fields, all of the tests in a single Criteria row or Or row must be true for a record to be included in the recordset

When you specify multiple criteria for a field and separate the criteria by a logical OR operator, only one of the criteria must be true for the record to be selected

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

When you use multiple Or rows and the criteria in any one of the Or rows is true, the record will be selected

DEMO

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Predicate Description

BETWEEN Useful for specifying a range of values

INUseful for specifying a list of values, any one of which can match the field being searched.

LIKE

Useful for searching for patterns in text fields. You can include special characters and ranges of values in the LIKE comparison string to define the character pattern you want.

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Special Character

Description

?Indicate any single character in that position

*Indicate zero or more characters in that position

# A single numeric digit in that position.

[ ]To test for a particular range of characters in a position

! Indicate exceptions

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Example:

Between 10 And 20 is the same as specifying >=10 And <=20

In ("WA", "CA", "ID") is the same as"WA" Or "CA" Or "ID"

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Example:

Like "?[a-k]d[0-9]*" tests for any single character in the first position, any character from a through k in the second position, the letter d in the third position, any character from 0 through 9 in the fourth position, and any number of characters after that

DEMO

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

You must always surround date and time values with pound signs (#)

Example:

#April 15, 1997#, #4/15/97#, and #15-Apr-1997# are all the same date

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Function Description

Day (date)Returns a value from 1 through 31 for the day of the month

Month (date)Returns a value from 1 through 12 for the month of the year

Year (date)Returns a value from 100 through 9999 for the year

Weekday (date)

As a default, returns a value from 1 (Sunday) through 7 (Saturday) for the day of the week

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Function Description

Hour(date)Returns a value from 0 through 23 for the hour of the day

DatePart (interval, date)

Returns a portion of the date or time, depending on the interval code you supply. Useful interval codes are "q" for quarter of the year (1 through 4) and "ww" for week of the year (1 through 53).

Date Returns the current system date.

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Examples:

To select records with OrderDate values after the 10th of any month, enter

Day([OrderDate]) as the calculated field

>10 as the criterion

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Examples:

To find all records that have a OrderDatevalue of June, enter

Month([OrderDate]) as the calculated field

6 as the criterion

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Examples:

To find dates in 1997, enter

Year([OrderDate]) as the calculated field

1997 as the criterion

To find business day dates, enter

Weekday([OrderDate]) as the calculated field

Between 2 and 6 as the criterion

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Examples:

To find a scheduled start time before noon, enter

Hour([OrderDate]) as the calculated field

>12 as the criterion

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table Examples:

To select dates in the second quarter, enter DatePart ("q", [OrderDate]) as the

calculated field

2 as the criterion

To select dates more than 30 days ago, enter <Date() -30 as the criterion

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Operator Description

+ Adds two numeric expressions.

_Subtracts the second numeric expression from the first numeric expression.

* Multiplies two numeric expressions.

/Divides the first numeric expression by the second numeric expression.

\

Rounds both numeric expressions to integers and then divides the first integer by the second integer. The result is truncated to an integer.

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Operator Description

^Raises the first numeric expression to the power indicated by the second numeric expression.

MODRounds both numeric expressions to integers, divides the first integer by the second integer, and returns the remainder.

&

Creates an extended text string by concatenating the first text string to the second text string. If either expression is a number, Access converts it to a text string before concatenating the expressions.

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Example: A field containing the group name, followed by a

single field containing the street address, a comma and a blank space, the city, another comma and a blank space, the state or province followed by two blank spaces, and the postal code

[StreetAddress] & ", " & [City] & ", " & [StateOrProvince] & " " & [PostalCode]

DEMO

ISM 322 – Database Theory and Applications © sfreires

Click in an empty field in the design grid, and then click the Build button on the toolbar

Selecting Data from a Single Table

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

Example:

Ccur([OrderDetails].UnitPrice* [Quantity]*(1-[Discount])/100)*100)

DEMO

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

You can add sorting information to determine the sequence of the data in a query

Click in the Sort row for the field you want to sort, and select Ascending or Descending from the drop-down list

DEMO

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

To calculate totals within any query, click the Totals button on the toolbar in Design view to open the Total row in the design grid

Access displays Group By in the Total row for any fields you already have in the design grid

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

In the drop-down list for the Total row in the design grid, you'll also find an Expression setting

Select this when you want to create an expression in the Total row that uses one or more of the total functions

DEMO

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

You might not want to include some records in the groups that form your total query

Select the Where setting in the Total row, clear that field's Show check box, and enter criteria that tell Access which records to exclude

DEMO

ISM 322 – Database Theory and Applications © sfreires

Selecting Data from a Single Table

You can also filter out groups of totals after the query has calculated the groups

To do this, enter criteria for any field that has a Group By setting, one of the total functions, or an expression using the total functions in its Total row

DEMO

Action Queries

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Testing with a Select Query

First create a select query using criteria that select the records you want to update

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Converting a Select Query to an Update Query

When you first create a query, Access creates a select query by default

You can find commands for the four types of action queries – make-table, update, append and delete – on the Query menu

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Converting a Select Query to an Update Query

You can also select one of these options from the Query Type toolbar button’s drop-down list

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Converting a Select Query to an Update Query

Select Update Query to convert the select query to an update query

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Converting a Select Query to an Update Query

Access changes the title bar of the Query window in Design view

It adds a row labeled Update To to the design grid

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Converting a Select Query to an Update Query

Use this row to specify how you want your data changed

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Running an Update Query

Make a backup copy of your table before you run an update query

Go to the Database window

Select the table you are about to update

Choose the Copy command from the Edit menu

Choose the Paste command from the Edit menu and give the copy of your table a different name

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Running an Update Query

To run the query, choose the Runcommand from the Query menu

Or click the Run button on the toolbar

Access first scans your table to determine how many rows will change based on your selection criteria

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Running an Update Query

It then displays a confirmation dialog box

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Running an Update Query

In the Database window, Access distinguishes action queries from select queries

Access displays a special icon, followed by an exclamation point before action query names

ISM 322 – Database Theory and Applications © sfreires

Updating Groups of Rows

Updating Multiple Fields

You can ask Access to update any or all of the fields in the record by including them in the design grid and specifying an update formula

You can also update one field by using a formula that is based on a different field in the record

ISM 322 – Database Theory and Applications © sfreires

Creating a New Table

Creating a Make-Table Query

As with most action queries, start with a select query to verify what you’re working with

To convert this to a make-table query, choose Make-Table Query from the Querymenu

ISM 322 – Database Theory and Applications © sfreires

Creating a New Table

Creating a Make-Table Query

Access displays the Make Table dialog box

ISM 322 – Database Theory and Applications © sfreires

Creating a New Table

Creating a Make-Table Query

Type in an appropriate name for the table you are creating

Click OK to close the dialog box

ISM 322 – Database Theory and Applications © sfreires

Creating a New Table

Creating a Make-Table Query

At any time, you can change the name of the table your query creates

Choose the Properties command from the View menu whenever the query is in Design view and change the Destination Table property

ISM 322 – Database Theory and Applications © sfreires

Creating a New Table

Running a Make-Table Query

Run it by choosing Run from the Querymenu or by clicking the Run button on the toolbar

Access creates the records that will be inserted in the new table and displays a confirmation dialog box

ISM 322 – Database Theory and Applications © sfreires

Creating a New TableRunning a Make-Table Query

The dialog box informs you of how many rows you will be creating in the new table

Click the Yes button to create your new table and insert the rows

ISM 322 – Database Theory and Applications © sfreires

Creating a New Table

Running a Make-Table Query

Be reminded that Access copies only basic field attributes when creating a new table

You might want to correct field names or define formatting information

ISM 322 – Database Theory and Applications © sfreires

Inserting Data from Another Table

Using an append query, you can copy a selected set of information and insert it into another table

You can also use an append query to bring data from another source into your database

You can edit the data and insert it into an existing table

ISM 322 – Database Theory and Applications © sfreires

Inserting Data from Another Table

A disadvantage of an append query is that it is easier to run into errors either because

You are trying to reinsert data that is already there

The data you are adding does not match the data type you defined in the table

ISM 322 – Database Theory and Applications © sfreires

Inserting Data from Another Table

Creating an Append Query

Open the query in Design view

Choose Append Query from the Querymenu

Access will prompt you for the name of the table to receive the new rows

ISM 322 – Database Theory and Applications © sfreires

Inserting Data from Another Table

Creating an Append Query

Access adds an Append To row to let you specify to which field in the receiving table each column in this query is to be appended

When the field names match, Access automatically fills in the name

ISM 322 – Database Theory and Applications © sfreires

Inserting Data from Another Table

Running an Append Query

You can either start out by building a select query, running it, and then converting it to an append query

You can build the append query directly and then switch to Datasheet view from Design view to examine the data that the query will add

ISM 322 – Database Theory and Applications © sfreires

Inserting Data from Another Table

Running an Append Query

Note that once you click Yes, the only way to undo these changes is to go to the target table and either select and delete the rows manually or build a delete query to do it

ISM 322 – Database Theory and Applications © sfreires

Deleting Groups of Rows

Using a Delete Query

Create a delete query fro a select query by choosing the Delete command from the Query menu when your query is in Design view

Simply choose Run from the Query menu or click the Run button on the toolbar to delete the rows you specified

ISM 322 – Database Theory and Applications © sfreires

Deleting Groups of Rows

Using a Delete Query

Access selects the rows to be deleted and displays the confirmation dialog box

Click the Yes button to proceed with the deletion