Technology & Information Management Instructor: Michael Kremer, Ph.D. Database Program: Microsoft Access Series DESIGNING, BUILDING, AND USING DATABASES (BEGINNING MICROSOFT ACCESS, X405.4) Section 3
AGENDA 6. Introduction to Queries 7. SELECT Query
Introduction to Queries 6.
6. INTRODUCTION TO QUERIES 101 Class database ERD diagram
6.1 WHAT IS A QUERY? 102 So far, we have been concerned with getting data into the database (database design, table design). Now we are going in the opposite direction, retrieving data from the database in a useful and meaningful manner. Query tool is used to retrieve data, it functions like temporary glue to bring data together. Data remains stored in separate tables, but a query can bring data from multiple tables together. User poses a question, query tool scans all necessary tables, fetches the data, and returns a result set of back to the user. Query tool uses a specific language, called SQL (Structured Query Language).
6.1 WHAT IS A QUERY? 103 Query tool not only assembles data and returns it back to the user, queries can also analyze data, such us counting or summarizing data. Additionally, queries can also modify data. Manual filing system does not meet today s data search needs anymore. A computerized filing system makes this process much more powerful. Real power of a database: Capable of examining the data in any way the user requested it.
6.1 WHAT IS A QUERY? 104 Data returned is only in memory, it is not stored in the database. Only instructions how to get the data is stored (SQL command). Queries make the data more human, the scattered data in the tables is a very abstract, though efficient in concept. Query tool is a separate layer to bridge the gap between the raw data in the database and the end-user. Forms/Reports are additional tools to present data to the user. Forms and reports are based on queries. This is another testament how import queries are.
6.2 TYPES OF QUERIES 105 There are many types of queries in MS Access. Different kinds of queries are categorized based on the effect on the data: Select Query Total Query Action Query Crosstab SQL Select Query: Selecting data, specifying sort order, and filtering data. Total Query: Analyzing data by groups of data. Aggregate functions for are sum, minimum, maximum, average, count, standard deviation, and variance. Action Query: Making bulk changes to data or create tables with data. Crosstab Query: Presenting data in a pivot-table like format. SQL Query: Use SQL rather than using the graphical query interface.
6.2 TYPES OF QUERIES 106 SELECT Query Most common type of query. Selects information from one or more tables selecting fields and limiting the number of records by imposing criteria. Sort records in a different way than in a table. Sorting on one or more fields. Performs calculations and creates additional fields based on field values stored in the database (derived or calculated data). Data is returned in the form of a datasheet. It looks exactly like the datasheet view of a table. However, the data in this view is not stored, it is dynamically created with the aid of the query tool. It is called a dynaset.
6.2 TYPES OF QUERIES 107 MS Access comes with a query tool, the query designer tool. Query Designer Tool facilitates creating queries rather than writing SQL.
6.2 TYPES OF QUERIES 108 Single table query based on table tbl_customer. Multi-table query based on tables tbl_customer, tbl_pets, and tbl_visits.
6.2 TYPES OF QUERIES 109 Total Query Special version of a select query. User can analyze data by using the so-called aggregate functions, such as count or sum. Data will be analyzed for the entire table or for groups of records within the table. Count customers within each state. Group By state field, then within each group, use Count function.
6.2 TYPES OF QUERIES 110 Action Queries Action queries can actually make changes to the data, such as adding, deleting, or modifying data as well as creating tables. Action queries make changes in bulk, that is, for an entire group of records or for the entire table. Make-Table Query: Create table and insert data based on a query. Append Query: Insert data into a new or existing table. Update Query: Update existing data. Delete Query: Delete existing data.
6.2 TYPES OF QUERIES 111 Crosstab Query Present and analyze data in a matrix-like format. Row and column headers are generated using data. Intersecting cell value is based on an aggregate function. SQL Query For complex and special queries, the graphical query interface cannot be used anymore. Create a SQL query by typing plain SQL commands. Remember, all queries in MS Access are based on SQL.
6.3 CAPABILITIES OF QUERIES 112
6.3 CAPABILITIES OF QUERIES 113
SELECT Query 7.
7.1 CREATE A SELECT QUERY 114 To create in query in MS Access, follow these steps: Select the Create tab, then click on Query Design in the Queries section. Select the tables necessary for the query in the Show Table dialog box, click on Add, when done click on Close. Add fields, create expressions, specify criteria, and view the results in the dynaset. Save the query as a new query object, use the prefix qry and a meaningful name for the query object.
7.1 CREATE A SELECT QUERY 115 Query wizard offers a variety of query options.
7.1 CREATE A SELECT QUERY 116
7.1 CREATE A SELECT QUERY 117 Query designer interface is divided into two areas, very similar to the table designer interface. The upper area is called the Table/Query Entry area and the lower pane is the Design Grid area. Table/Query Entry Area: Tables and/or Queries are displayed as well as saved relationships. Query Design Grid: Select fields, sort and add criteria. Resize panes by dragging horizontal bar. Resize tables/queries, simply for organizational purposes. Table/Query Entry Area Design Grid Area
7.1 CREATE A SELECT QUERY 118 Rows in Query Design Grid: Field: To select fields (various methods). Table: The table name which a selected field belongs to. Sort: To sort records ascending, descending or not sorted. Show: To show a field in the resulting dynaset. Criteria: To specify one or more criteria for one field or multiple fields. Or: To specify multiple criteria using a Boolean operator of or. The design grid s column represents the fields of the query in the datasheet view once the query is run. The order of fields is from left to right.
7.1 CREATE A SELECT QUERY 119 Each of the output fields has properties, very similar to fields in a table. To access these query field properties: Right-click in the column and select properties from the shortcut-menu. Click in the column and then click the Property Sheet button in the contextual tab Query Tools, Design. Property Sheet Button Short-Cut Menu, Properties
7.1 CREATE A SELECT QUERY 120 Property sheet shows some table field properties which can be overridden at the query level for this particular query only. The field property settings at the table level remain intact.
7.2 WORKING WITH FIELDS IN DESIGN GRID 121 Selecting Fields in Design Grid Area Several methods to add fields from the Query/Table Entry pane to the Design Grid pane. Selecting fields in the tables is similar to selecting files in Windows Explorer. The use of the [Shift] and the [Ctrl] keys is crucial for this operation. Most commonly used method: Select one field in a table, hold the mouse button, and drag the field into the Design Grid pane. Mouse pointer shape changes to a Field Icon (rectangle) with a plus sign. When dragging a selecting field over the Table/Query Entry pane, a circle with a backslash No-Sign, Prohibition Sign is displayed, indicating that you cannot drop a field here. Field Icon with Plus Sign
7.2 WORKING WITH FIELDS IN DESIGN GRID 122 Field Selection Methods: Double-clicking on a field will add this field automatically to the Design Grid pane into the next available column. Selecting multiple adjacent fields: Click with the mouse on the first field, hold down the [Shift] key and select the last field of the range. Click in the orange highlighted area and drag it into the design grid area. Release the mouse button in the Design Grid area. Selecting multiple, non-adjacent fields: Select the first field, then hold down the [Ctrl], then select the other fields. Then drag it into the design grid pane. Release the mouse button in the Design Grid area. Asterisk functions like a wildcard character, double-clicking on it transfers all fields into the Design Grid area. Click in the field row of the Design Grid area, a little drop-down arrow appears to the right. Clicking on this arrow lists all available fields.
7.2 WORKING WITH FIELDS IN DESIGN GRID 123 Managing Fields in Design Grid Area Change the field order in the Design Grid pane: Move the mouse to the top of the column (small gray area above the field row) to be moved. The mouse pointer shape changes to a vertical arrow. Now click and hold the mouse button while you drag the mouse to the left or right. A vertical bar moves with the mouse movement, position the bar to the new position and the release the mouse button. The column will be moved to the new position. Field Selection Arrow Field Selected
7.2 WORKING WITH FIELDS IN DESIGN GRID 124 To remove a field, simply select the column as described above and hit the [Del] button to delete the column. To insert a field, drag a field from the Table/Query Entry pane and place it in the Design Grid pane at the desired location. Existing fields will be moved automatically to the right to accommodate the new field. To show a field in the datasheet view, select the checkbox in the Show row of the Design Grid pane. This checkbox is turned on by default. Field Names/Column Header in a Query When you view a query in datasheet view, the column headers are usually the field names as defined at the table level. Caption property in table design and query design allows you to change column header.
7.2 WORKING WITH FIELDS IN DESIGN GRID 125
7.2 WORKING WITH FIELDS IN DESIGN GRID 126 Export data from a query: Caption property does not get exported, actual field names are used. This is an important fact: Database engine properties vs. application layer properties. Export takes place at the database level. Caption property is part of the application layer.
7.2 WORKING WITH FIELDS IN DESIGN GRID 127 Sorting Data in a Query To sort on a specific field, click inside the sort row of that field. Three choices appear in a drop- down list: Ascending, Descending, and (not sorted). By default, a query is sorted by the primary key of the underlying table. To change the sort order, select one or more fields to be sorted. Sort order will be processed from left to right. Sort Options
7.2 WORKING WITH FIELDS IN DESIGN GRID 127
7.2 WORKING WITH FIELDS IN DESIGN GRID 129
7.3 SIMPLE CRITERIA EXPRESSIONS IN QUERIES 130 To vertically limit the number of records, specify one or more criteria in the criteria row of the query design grid in the query interface. Typical criteria expressions could be: Pets Table: Show me all female pets only. Pets Table: Retrieve a list of pets born before January 1990. Customer and Pets Table: Show me all dogs from customers in California.
7.3 SIMPLE CRITERIA EXPRESSIONS IN QUERIES 131 To enter a criterion for a text field, simply type a value in the criteria row for that field. MS Access will automatically enclose the criterion in delimiters depending on data type of field: Text Quotation marks ( ) Number No delimiters Date/Time Pound symbol (#) The equal sign is the default relational operator; therefore it does not have to be typed in the criteria row. When you enter MS Access displays and retrieves records where Borderville Borderville City field value equals Borderville 1 1 TypeOfCustomer field value equals 1 < 1/1/1998 < #1/1/1998# CustomerSince field value is less than 1/1/1998 Tall Pines Tall Pines City field value equals Tall Pines Between 1/1/1995 AND 1/1/2000 Between #1/1/1995# AND #1/1/2000# CustomerSince field values between 1/1/1995 and 1/1/2000, inclusive
7.3 SIMPLE CRITERIA EXPRESSIONS IN QUERIES 132
7.3 SIMPLE CRITERIA EXPRESSIONS IN QUERIES 133
7.3 SIMPLE CRITERIA EXPRESSIONS IN QUERIES 134 Yes/No fields: Yes values: True, On, Not No, <>No, <No, -1 No values: False, Off, Not Yes, <>Yes, >Yes, 0 A Yes/No field is stored as -1/0 in the database.
7.3 SIMPLE CRITERIA EXPRESSIONS IN QUERIES 135 Creating a query using a Lookup Wizard relationship:
7.3 SIMPLE CRITERIA EXPRESSIONS IN QUERIES 135 Creating a query using a Lookup Wizard relationship: