microsoft access a hands-on introduction chapter 3

27
Microsoft Access Microsoft Access A Hands-On Introduction A Hands-On Introduction Chapter 3 Chapter 3

Upload: meredith-mosley

Post on 17-Jan-2016

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Microsoft Access A Hands-On Introduction Chapter 3

Microsoft AccessMicrosoft Access

A Hands-On IntroductionA Hands-On Introduction

Chapter 3Chapter 3

Page 2: Microsoft Access A Hands-On Introduction Chapter 3

22

Establishing RelationshipsEstablishing Relationships

A relationship is a link between two tables that establishes A relationship is a link between two tables that establishes an association between fields in the two tables.an association between fields in the two tables.

The requirement is that the link must be between The requirement is that the link must be between compatible data.compatible data.

Relationships are what differentiate tables in Relational Relationships are what differentiate tables in Relational Databases from flat files.Databases from flat files.

They are also the mechanism by which queries can provide They are also the mechanism by which queries can provide us with answers to questions such as “What is the total us with answers to questions such as “What is the total dollar amount of Genoa Salami I purchased in July” or dollar amount of Genoa Salami I purchased in July” or “How many unpaid invoices are there for customer 55317”.“How many unpaid invoices are there for customer 55317”.

Page 3: Microsoft Access A Hands-On Introduction Chapter 3

33

Types of RelationshipsTypes of Relationships

One to one.One to one. - For each record - For each record

in the Employee in the Employee table, there is one table, there is one record in the record in the Payroll table.Payroll table.

Not frequently Not frequently used, however used, however very useful in very useful in some some circumstances.circumstances.

Page 4: Microsoft Access A Hands-On Introduction Chapter 3

44

Types of Relationships (Contd.)Types of Relationships (Contd.)

One-to-many.One-to-many. - Most common type.- Most common type. For each record on the one For each record on the one

side there can be zero, one side there can be zero, one or more records on the or more records on the many side.many side.

Created by linking the Created by linking the Primary key on the one Primary key on the one side to a compatible field(s) side to a compatible field(s) on the many side. The on the many side. The linked field on the many linked field on the many side is sometimes called side is sometimes called the the Foreign Key.Foreign Key.

Page 5: Microsoft Access A Hands-On Introduction Chapter 3

55

Types of Relationships (Contd.)Types of Relationships (Contd.)

Many-to-many.Many-to-many. - Less common than one-- Less common than one-

to- many.to- many. Actually occurs via two Actually occurs via two

one-to-many.one-to-many. In the example shown, In the example shown,

would allow us to create a would allow us to create a query to determine what query to determine what movies are in Category movies are in Category “Action” as well as what “Action” as well as what are the categories for the are the categories for the movie “101 Dalmatians”.movie “101 Dalmatians”.

Page 6: Microsoft Access A Hands-On Introduction Chapter 3

66

Creating RelationshipsCreating Relationships

We will now open the Microsoft Access sample database and We will now open the Microsoft Access sample database and use the Relationships window to create relationships between use the Relationships window to create relationships between several tables.several tables.

It is typical when designing and creating a database, that all It is typical when designing and creating a database, that all relationships are created prior to populating the tables. There relationships are created prior to populating the tables. There are several reasons for this that we will discuss shortly.are several reasons for this that we will discuss shortly.

Please follow along with your instructor as we use the Nwind Please follow along with your instructor as we use the Nwind

database and learn how this is done.database and learn how this is done.

Page 7: Microsoft Access A Hands-On Introduction Chapter 3

77

Referential IntegrityReferential Integrity

Ensures that an order record Ensures that an order record cannot be added unless cannot be added unless there is a parent in the there is a parent in the Customers table.Customers table.

Ensures that a Customer Ensures that a Customer record cannot be deleted if record cannot be deleted if there are child records in the there are child records in the Orders table.Orders table.

The overall goal is to protect The overall goal is to protect against accidental data loss.against accidental data loss.

Also ensures against having Also ensures against having orphaned records.orphaned records.

Page 8: Microsoft Access A Hands-On Introduction Chapter 3

88

Referential Integrity (contd.)Referential Integrity (contd.)

The Cascade Delete checkbox if The Cascade Delete checkbox if checked causes the deletion of checked causes the deletion of records on the many side if the records on the many side if the parent record on the one side is parent record on the one side is deleted.deleted.

If Cascade Update is checked, If Cascade Update is checked, Access will update the matching Access will update the matching foreign key value if the Primary foreign key value if the Primary key value is changed in the key value is changed in the parent record. This option is used parent record. This option is used infrequently as changing foreign infrequently as changing foreign keys is not a good idea.keys is not a good idea.

Page 9: Microsoft Access A Hands-On Introduction Chapter 3

99

Using the DatasheetUsing the Datasheet The Datasheet is a built in tool for viewing, adding and updating data. Please The Datasheet is a built in tool for viewing, adding and updating data. Please

follow along with your instructor as we explore its capabilities. follow along with your instructor as we explore its capabilities.

Page 10: Microsoft Access A Hands-On Introduction Chapter 3

1010

Using the Datasheet (Contd.)Using the Datasheet (Contd.)

Now that we have seen what can be accomplished with the Now that we have seen what can be accomplished with the Datasheet, let’s take it for a test drive.Datasheet, let’s take it for a test drive.

First, let’s try sorting in descending sequence on Unit Price. First, let’s try sorting in descending sequence on Unit Price. What is our most expensive item?What is our most expensive item?

Next, let’s sort on the Category field ascending. How many Next, let’s sort on the Category field ascending. How many Produce items do we sell?Produce items do we sell?

Who can tell me how many different items are sold in Who can tell me how many different items are sold in boxes?boxes?

Let’s change the price of Filo Mix to $8.00. What happened Let’s change the price of Filo Mix to $8.00. What happened to the Record Pointer icon when you made the change?to the Record Pointer icon when you made the change?

Click anywhere in the Reorder Level field. What do you see Click anywhere in the Reorder Level field. What do you see in the lower left of the Status Bar?in the lower left of the Status Bar?

Finally, add several new records to the Products table.Finally, add several new records to the Products table.

Page 11: Microsoft Access A Hands-On Introduction Chapter 3

1111

Using the Datasheet (Contd.)Using the Datasheet (Contd.)

As you have seen, the Datasheet is great for viewing data As you have seen, the Datasheet is great for viewing data due to it’s ability to do sorting and filtering.due to it’s ability to do sorting and filtering.

Another benefit of the Datasheet is the ability to get a broad Another benefit of the Datasheet is the ability to get a broad picture of your data since many records are displayed at picture of your data since many records are displayed at once. This benefit however tends to make doing record once. This benefit however tends to make doing record maintenance more difficult since you can easily lose track of maintenance more difficult since you can easily lose track of where you are.where you are.

Later, we will work on designing forms which are a much Later, we will work on designing forms which are a much better tool for doing data maintenance and additions.better tool for doing data maintenance and additions.

Page 12: Microsoft Access A Hands-On Introduction Chapter 3

1212

What is a Query?What is a Query? A Query is a request to your database to return specific information A Query is a request to your database to return specific information

from one or more of it’s tables.from one or more of it’s tables. Below is a screenshot of the Below is a screenshot of the QQuery uery BBy y EExample window that asks the xample window that asks the

question “What customers do I have in Madrid Spain?” question “What customers do I have in Madrid Spain?” The four field names at the bottom were selected from the Customers The four field names at the bottom were selected from the Customers

table field list at the top.table field list at the top. Note that the Title Bar states that this is a Select Query. Select Queries Note that the Title Bar states that this is a Select Query. Select Queries

are the most common.are the most common.

Page 13: Microsoft Access A Hands-On Introduction Chapter 3

1313

What is a Query? (Contd.)What is a Query? (Contd.) Below are the results of our Select Query which has been Below are the results of our Select Query which has been

returned as something called a Recordset.returned as something called a Recordset. A Recordset is a collection of one or more rows of data that 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 looks like a table. We see only four fields because that is what we requested from the Customers table.what we requested from the Customers table.

A Recordset is a very frequently used object in Access and A Recordset is a very frequently used object in Access and we will be seeing more on them later.we will be seeing more on them later.

Page 14: Microsoft Access A Hands-On Introduction Chapter 3

1414

What is a Query? (Contd.)What is a Query? (Contd.)

By selectively choosing what you want to see, you only get By selectively choosing what you want to see, you only get records returned that match the Criteria that you specified.records returned that match the Criteria that you specified.

In effect, we have filtered out what we wished to see and In effect, we have filtered out what we wished to see and removed what we did not want to see.removed what we did not want to see.

Our Recordset is a separate object from the Customers table Our Recordset is a separate object from the Customers table although it is considered a logical view of certain rows in the although it is considered a logical view of certain rows in the table.table.

In this case, all the information we needed came from one In this case, all the information we needed came from one table. Sometimes however, the information we need comes table. Sometimes however, the information we need comes from more than one table and may even come from more than from more than one table and may even come from more than one database.one database.

Page 15: Microsoft Access A Hands-On Introduction Chapter 3

1515

Other things we can do with Select QueriesOther things we can do with Select Queries

Sorting the results on one or more fields.Sorting the results on one or more fields. Perform calculations. For example, calculating total cost by Perform calculations. For example, calculating total cost by

multiplying Unit Cost by Quantity Ordered.multiplying Unit Cost by Quantity Ordered. Creating a field called MiddleInitial by extracting the first letter Creating a field called MiddleInitial by extracting the first letter

of the MiddleName field.of the MiddleName field. Select Queries are also frequently used as a data source for Select Queries are also frequently used as a data source for

other Access objects such as forms, reports and even other other Access objects such as forms, reports and even other queries.queries.

Queries are arguably the most important feature of Relational Queries are arguably the most important feature of Relational Databases.Databases.

Page 16: Microsoft Access A Hands-On Introduction Chapter 3

1616

Types of QueriesTypes of Queries

Select queriesSelect 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 queriesAction queries

- Queries that update tables, delete rows from tables, append - Queries that update tables, delete rows from tables, append data to tables and also make new tablesdata to tables and also make new tables

Crosstab queriesCrosstab queries

- Data summarization- Data summarization SQL Specific queriesSQL Specific queries

Page 17: Microsoft Access A Hands-On Introduction Chapter 3

1717

Types of Action QueriesTypes of Action Queries

Make-Table QueryMake-Table Query

- Creates a new table from one or more existing tables. - Creates a new table from one or more existing tables. For example: Create a Paid Invoice table from the current For example: Create a Paid Invoice table from the current Invoice table by selecting all records where the Invoice table by selecting all records where the AmountPaid field equals the AmountOwed field.AmountPaid field equals the AmountOwed field.

Delete QueryDelete Query

- Deletes all records that match a set of Criteria.- Deletes all records that match a set of Criteria. Append QueryAppend Query

- Appends to an existing table, records that match a set of - Appends to an existing table, records that match a set of Criteria.Criteria.

Update QueryUpdate Query

- Updates records based on a set of Criteria.- Updates records based on a set of Criteria.

Page 18: Microsoft Access A Hands-On Introduction Chapter 3

1818

SQL Specific QueriesSQL Specific Queries

Union queryUnion query

- Combine matching fields from two or more tables.- Combine matching fields from two or more tables. Passthrough queriesPassthrough queries

- Sends SQL commands directly to an ODBC data source. - Sends SQL commands directly to an ODBC data source. Allows direct SQL specific connection with a Client Server Allows direct SQL specific connection with a Client Server database.database.

Data-definition queryData-definition query

- Queries that deal with database structure, not with data - Queries that deal with database structure, not with data tables. These types of queries are typically used only by tables. These types of queries are typically used only by Database Administrators.Database Administrators.

Page 19: Microsoft Access A Hands-On Introduction Chapter 3

1919

Working with Select QueriesWorking with Select Queries Selecting ranges of recordsSelecting 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

Page 20: Microsoft Access A Hands-On Introduction Chapter 3

2020

Working with Select Queries (Contd.)Working with Select Queries (Contd.) This is a multi-table query in the sample Nwind databaseThis is a multi-table query in the sample Nwind database Notice that all the tables have join lines. These indicate Notice that all the tables have join lines. These indicate

Relationships and are necessary for the query to work.Relationships and are necessary for the query to work.

Page 21: Microsoft Access A Hands-On Introduction Chapter 3

2121

Using Expressions in QueriesUsing Expressions in Queries Expressions are a combination of symbols, identifiers, operators and Expressions are a combination of symbols, identifiers, operators and

values that together produce a result.values that together produce a result. - Examples- Examples [Quantity] * [UnitCost][Quantity] * [UnitCost] [TotalCost] + [Freight][TotalCost] + [Freight] Expressions can be used in queries as Criteria.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.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)

Page 22: Microsoft Access A Hands-On Introduction Chapter 3

2222

Additional Criteria ExamplesAdditional Criteria Examples The “Like” operator shown here The “Like” operator shown here

finds all records where the finds all records where the “ContactTitle” field contains the “ContactTitle” field contains the three letters “man”. three letters “man”.

Another version of this expression Another version of this expression would substitute “Not” for “Like”. would substitute “Not” for “Like”. This would exclude all the records This would exclude all the records containing the same three letters containing the same three letters and instead, return all the others.and instead, return all the others.

The “In” operator checks against The “In” operator checks against several choices. For example the several choices. For example the expression: In (“Chicago”, expression: In (“Chicago”, “Bangor”, “Rome”) would return all “Bangor”, “Rome”) would return all records where the “City” field records where the “City” field contained any one of those three contained any one of those three names.names.

Page 23: Microsoft Access A Hands-On Introduction Chapter 3

2323

Parameter QueriesParameter Queries Lets take a look at a Select query that asks for all Products that have a 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 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 return that result. Suppose however that after running the query and getting the results, we decide that we wish to use different amounts. the results, we decide that we wish to use different amounts.

We need to create another query. Or do we.We need to create another query. Or do we.

Page 24: Microsoft Access A Hands-On Introduction Chapter 3

2424

Parameter Queries (Contd.)Parameter Queries (Contd.) Shown below is a Parameter query which answers the dilemma shown Shown below is a Parameter query which answers the dilemma shown

in the previous slide. This query can be saved and run many times 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 since it asks users at run time to enter the lower and upper ranges of UnitCost as Criteria.UnitCost as Criteria.

Page 25: Microsoft Access A Hands-On Introduction Chapter 3

2525

Parameter Queries (Contd.)Parameter Queries (Contd.) Microsoft Access identifies this as a Parameter query because of the text Microsoft Access identifies this as a Parameter query because of the text

entered between square brackets in the Criteria field. Because there are entered between square brackets in the Criteria field. Because there are two sets of brackets, Access asks two questions using the text that you 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. see. It then captures the users response and uses the values for Criteria. Parameter queries are very useful objects and very common.Parameter queries are very useful objects and very common.

Page 26: Microsoft Access A Hands-On Introduction Chapter 3

2626

Parameter Queries (Contd.)Parameter Queries (Contd.)

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

Page 27: Microsoft Access A Hands-On Introduction Chapter 3

2727

What We Have CoveredWhat We Have Covered

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