Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft Access A Hands-On Introduction Chapter 3.

Similar presentations


Presentation on theme: "Microsoft Access A Hands-On Introduction Chapter 3."— Presentation transcript:

1 Microsoft Access A Hands-On Introduction Chapter 3

2 2 Establishing Relationships  A relationship is a link between two tables that establishes an association between fields in the two tables.  The requirement is that the link must be between compatible data.  Relationships are what differentiate tables in Relational Databases from flat files.  They are also the mechanism by which queries can provide us with answers to questions such as “What is the total dollar amount of Genoa Salami I purchased in July” or “How many unpaid invoices are there for customer 55317”.

3 3 Types of Relationships  One to one.  - For each record in the Employee table, there is one record in the Payroll table.  Not frequently used, however very useful in some circumstances.

4 4 Types of Relationships (Contd.)  One-to-many.  - Most common type.  For each record on the one side there can be zero, one or more records on the many side.  Created by linking the Primary key on the one side to a compatible field(s) on the many side. The linked field on the many side is sometimes called the Foreign Key.

5 5 Types of Relationships (Contd.)  Many-to-many.  - Less common than one- to- many.  Actually occurs via two one-to-many.  In the example shown, would allow us to create a query to determine what movies are in Category “Action” as well as what are the categories for the movie “101 Dalmatians”.

6 6 Creating Relationships  We will now open the Microsoft Access sample database and use the Relationships window to create relationships between several tables.  It is typical when designing and creating a database, that all relationships are created prior to populating the tables. There are several reasons for this that we will discuss shortly.  Please follow along with your instructor as we use the Nwind database and learn how this is done.

7 7 Referential Integrity  Ensures that an order record cannot be added unless there is a parent in the Customers table.  Ensures that a Customer record cannot be deleted if there are child records in the Orders table.  The overall goal is to protect against accidental data loss.  Also ensures against having orphaned records.

8 8 Referential Integrity (contd.)  The Cascade Delete checkbox if checked causes the deletion of records on the many side if the parent record on the one side is deleted.  If Cascade Update is checked, Access will update the matching foreign key value if the Primary key value is changed in the parent record. This option is used infrequently as changing foreign keys is not a good idea.

9 9 Using the Datasheet  The Datasheet is a built in tool for viewing, adding and updating data. Please follow along with your instructor as we explore its capabilities.

10 10 Using the Datasheet (Contd.)  Now that we have seen what can be accomplished with the Datasheet, let’s take it for a test drive.  First, let’s try sorting in descending sequence on Unit Price. What is our most expensive item?  Next, let’s sort on the Category field ascending. How many Produce items do we sell?  Who can tell me how many different items are sold in boxes?  Let’s change the price of Filo Mix to $8.00. What happened to the Record Pointer icon when you made the change?  Click anywhere in the Reorder Level field. What do you see in the lower left of the Status Bar?  Finally, add several new records to the Products table.

11 11 Using the Datasheet (Contd.)  As you have seen, the Datasheet is great for viewing data due to it’s ability to do sorting and filtering.  Another benefit of the Datasheet is the ability to get a broad picture of your data since many records are displayed at once. This benefit however tends to make doing record maintenance more difficult since you can easily lose track of where you are.  Later, we will work on designing forms which are a much better tool for doing data maintenance and additions.

12 12 What is a Query?  A Query is a request to your database to return specific information from one or more of it’s tables.  Below is a screenshot of the Query By Example window that asks the question “What customers do I have in Madrid Spain?”  The four field names at the bottom were selected from the Customers table field list at the top.  Note that the Title Bar states that this is a Select Query. Select Queries are the most common.

13 13 What is a Query? (Contd.)  Below are the results of our Select Query which has been returned as something called a Recordset.  A Recordset is a collection of one or more rows of data that looks like a table. We see only four fields because that is what we requested from the Customers table.  A Recordset is a very frequently used object in Access and we will be seeing more on them later.

14 14 What is a Query? (Contd.)  By selectively choosing what you want to see, you only get records returned that match the Criteria that you specified.  In effect, we have filtered out what we wished to see and removed what we did not want to see.  Our Recordset is a separate object from the Customers table although it is considered a logical view of certain rows in the table.  In this case, all the information we needed came from one table. Sometimes however, the information we need comes from more than one table and may even come from more than one database.

15 15 Other things we can do with Select Queries  Sorting the results on one or more fields.  Perform calculations. For example, calculating total cost by multiplying Unit Cost by Quantity Ordered.  Creating a field called MiddleInitial by extracting the first letter of the MiddleName field.  Select Queries are also frequently used as a data source for other Access objects such as forms, reports and even other queries.  Queries are arguably the most important feature of Relational Databases.

16 16 Types of Queries  Select queries - Most commonly used query - Most commonly used query - Returns a Recordset which is updatable - Returns a Recordset which is updatable - Usually displayed in Datasheet format - Usually displayed in Datasheet format - Commonly used as input for forms and reports - Commonly used as input for forms and reports  Action queries - Queries that update tables, delete rows from tables, append data to tables and also make new tables - Queries that update tables, delete rows from tables, append data to tables and also make new tables  Crosstab queries - Data summarization - Data summarization  SQL Specific queries

17 17 Types of Action Queries  Make-Table Query - Creates a new table from one or more existing tables. For example: Create a Paid Invoice table from the current Invoice table by selecting all records where the AmountPaid field equals the AmountOwed field. - Creates a new table from one or more existing tables. For example: Create a Paid Invoice table from the current Invoice table by selecting all records where the AmountPaid field equals the AmountOwed field.  Delete Query - Deletes all records that match a set of Criteria. - Deletes all records that match a set of Criteria.  Append Query - Appends to an existing table, records that match a set of Criteria. - Appends to an existing table, records that match a set of Criteria.  Update Query - Updates records based on a set of Criteria. - Updates records based on a set of Criteria.

18 18 SQL Specific Queries  Union query - Combine matching fields from two or more tables. - Combine matching fields from two or more tables.  Passthrough queries - Sends SQL commands directly to an ODBC data source. Allows direct SQL specific connection with a Client Server database. - Sends SQL commands directly to an ODBC data source. Allows direct SQL specific connection with a Client Server database.  Data-definition query - Queries that deal with database structure, not with data tables. These types of queries are typically used only by Database Administrators. - Queries that deal with database structure, not with data tables. These types of queries are typically used only by Database Administrators.

19 19 Working with Select Queries  Selecting ranges of records - Choose the range you wish to see - Choose the range you wish to see - Define the Criteria (= <> = Between … And) - Define the Criteria (= <> = Between … And) - Additional Criteria with And, Not, Or - Additional Criteria with And, Not, Or - Sorting - Sorting

20 20 Working with Select Queries (Contd.)  This is a multi-table query in the sample Nwind database  Notice that all the tables have join lines. These indicate Relationships and are necessary for the query to work.

21 21 Using Expressions in Queries  Expressions are a combination of symbols, identifiers, operators and values that together produce a result. - Examples - Examples [Quantity] * [UnitCost] [Quantity] * [UnitCost] [TotalCost] + [Freight] [TotalCost] + [Freight]  Expressions can be used in queries as Criteria. - Examples - Examples Between #1/1/1997# And #12/31/1997# Between #1/1/1997# And #12/31/1997# >”L” >”L”  Expressions can also be used in queries to create fields. - Examples - Examples MiddleInitial: (Left([Middlename],1)) MiddleInitial: (Left([Middlename],1)) DueDate: ([ShippedDate]+30) DueDate: ([ShippedDate]+30)

22 22 Additional Criteria Examples  The “Like” operator shown here finds all records where the “ContactTitle” field contains the three letters “man”.  Another version of this expression would substitute “Not” for “Like”. This would exclude all the records containing the same three letters and instead, return all the others.  The “In” operator checks against several choices. For example the expression: In (“Chicago”, “Bangor”, “Rome”) would return all records where the “City” field contained any one of those three names.

23 23 Parameter Queries  Lets take a look at a Select query that asks for all Products that have a UnitPrice between $2.25 and $4.75. Shown below is an example that will return that result. Suppose however that after running the query and getting the results, we decide that we wish to use different amounts.  We need to create another query. Or do we.

24 24 Parameter Queries (Contd.)  Shown below is a Parameter query which answers the dilemma shown in the previous slide. This query can be saved and run many times since it asks users at run time to enter the lower and upper ranges of UnitCost as Criteria.

25 25 Parameter Queries (Contd.)  Microsoft Access identifies this as a Parameter query because of the text entered between square brackets in the Criteria field. Because there are two sets of brackets, Access asks two questions using the text that you see. It then captures the users response and uses the values for Criteria. Parameter queries are very useful objects and very common.

26 26 Parameter Queries (Contd.)  On the right are the Message boxes presented to the user one at a time starting with the upper one. The user answers the first one by putting the lower range price into the text area of the box and clicks OK. The lower Message box is then displayed and the user responds with the upper range price and clicks OK. The query runs and returns the results.

27 27 What We Have Covered  Table Relationships.  Referential Integrity.  Datasheets.  What are Queries and what are they used for.  The various types of Queries.  Select Queries.  Parameter Queries.  Using Expressions in Queries.


Download ppt "Microsoft Access A Hands-On Introduction Chapter 3."

Similar presentations


Ads by Google