Inner Joins Objectives: Creating Queries with data from Multiple Tables Joining two tables using an Inner Join Referential Data Integrity Cascade Update.

Slides:



Advertisements
Similar presentations
MS-Access XP Lesson 2. Input Mask Property 1.Field : Phone No Data Type : Number Input Mask : Character 0 represent a single digit and phone.
Advertisements

Concepts of Database Management Seventh Edition
Exploring Microsoft Access 2003 Chapter 6 Many-to-Many Relationships: A More Complex System.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 1 Committed to Shaping the Next Generation of IT Experts. Chapter 2: Relational Databases.
Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 3 1 Microsoft Office Access 2003 Tutorial 3 – Querying a Database.
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
Querying a Database Microsoft Office Access 2003.
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and Database Creation 1 CSE 2111 Introduction to Database.
Define Table Relationships—1 of 3 One of the most powerful features of a relational database management system, such as Access, is its ability to define.
Microsoft Access Lesson 3
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 1 1. Chapter 2: Relational Databases and Multi-Table Queries Exploring Microsoft Office.
1 Microsoft Access 2002 Tutorial 3 – Querying a Database.
With Microsoft Office 2007 Intermediate© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Intermediate.
With Microsoft Access 2007 Volume 1© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access 2007 Volume 1 Chapter.
Primary & Foreign Keys. PK & FK 1.Primary key is required 2.The PK must be unique 3.If the primary key from one table is related to a field in another.
Copyright © 2008 Pearson Prentice Hall. All rights reserved.1 1 Committed to Shaping the Next Generation of IT Experts. Chapter 2: Relational Databases.
CS&E 1111 AcQueries Writing Simple Queries in Access Displaying on specific data fields Filtering data using criteria Objectives: Learn how to use the.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
CS&E 1111 AcQueries Querying in Access Sorting data Aggregating Data Performing Calculations Objectives: Learn how to use the Access Query Design Tool.
CS&E 1111 AcInnerJoins Inner Joins Objectives: Creating Queries with data from Multiple Tables Joining two tables using an Inner Join Referential Data.
DAY 14: ACCESS CHAPTER 1 Tazin Afrin October 03,
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 9 – Building Links,
Office Management Tools II Ms Saima Gul.  When you create your tables, you should assign each table a primary key—one or more fields whose contents are.
Relationships and Advanced Query Concepts Using Multiple Tables Please use speaker notes for additional information!
Database Design. Referential Integrity : data in a table that links to data in another table must always work in such a way that following the link will.
Microsoft Office 2007 Access Chapter 3 Maintaining a Database.
Access Project 3 Notes. Introduction Maintaining the Database  Modifying the data to keep it up-to-date Restructure the Database  To change the database.
Microsoft Access Intro Class 6 Relationships.
Using Microsoft Access 56:150 Information System Design.
CSC 240 (Blum)1 Introduction to Data Entry, Queries and Reports.
Exploring Microsoft Access Chapter 6 Many-to-Many Relationships: A More Complex System.
Maintaining a Database Access Project 3. 2 What is Database Maintenance ?  Maintaining a database means modifying the data to keep it up-to-date. This.
Access Chapter 3-Obtaining Answers to Your Data Questions.
You can sort Access data so you can view records in the order you want to view them, and you can filter data so you only see the records you want to see.
Computer Science & Engineering 2111 Outer Joins 1CSE 2111 Lecture- Inner Vs. Outer Jioins.
Computer Science & Engineering 2111 Lecture 13 Outer Joins 1.
Exploring Microsoft Access Chapter 5 One-to-Many Relationships: Subforms and Multiple Table Queries.
Chapter 6 Many-to Many Relationship. Agenda AutoNumber Many-to-many relationship Cascaded updating and deleting Auto Lookup Parameter query Total query.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
Computer Science & Engineering 2111 Inner Joins and Advanced Queries 1CSE 2111 Lecture-Advanced Queries.
AOIT Database Design Unit 3, Lesson 9 Data Integrity Copyright © 2009–2011 National Academy Foundation. All rights reserved.
Database Relationships
Writing Simple Queries in Access
Tables & Relationships
Databases Chapter 9 Asfia Rahman.
Rob Gleasure robgleasure.com
Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations.
Objectives Query for top values Create a parameter query
and Defining Table Relationships
Objectives Create an action query to create a table
CIS 155 Table Relationship
Database Relationships
Introduction to Database Systems
Microsoft Access 2003 Illustrated Complete
Microsoft Office Access 2003
Tutorial 3 – Querying a Database
Please use speaker notes for additional information!
Microsoft Office Access 2003
Relational Queries (query 12) Display vendor contact info (contact person and phone number) for inventory products (relationship query) Query: Inventory.
Database Relationships
Nagendra Vemulapalli Access chapters 3&6 Nagendra Vemulapalli
Use of SQL – The Patricia database
Tutorial 9 Using Action Queries and Advanced Table Relationships
Relationships While we are on the subject of Relationships, let’s take a quick look at them.
Presentation transcript:

Inner Joins Objectives: Creating Queries with data from Multiple Tables Joining two tables using an Inner Join Referential Data Integrity Cascade Update & Cascade Delete Joining three tables Table with a Many-One-Many relationship Tables with a One-Many-One relationship

Let’s look at a database with 3 tables: What are the primary keys in these tables? What are the foreign key relationships we can setup? FruitList Received Shipped Shipped Received FruitList 1 8 1 8 Fruitid Qty Shipped Fruitid Qty Received Fruitid Fruit Price * Primary Keys

Using Multiple Tables in a Query Queries become much more powerful when you can combine information from 2 or more tables. We can combine the records in the two tables using an inner join operation using the following steps: Add both tables to the query design view Setup the appropriate relationships if they have not been done so on the relationship table. Choose the appropriate fields, criteria, sort, etc. Run your query and look at the results

Create a list of Fruit, by name/id and the quantity shipped. 8 Shipped FruitList 1 Fruitid Qty Shipped Fruitid Fruit Price What does the resulting dynaset look like?

The “inner join” results in a list of only records which have a matching record on the related table Fruitlist Joined of FruitID What happened to the shipment for fruitID 7? What happened to pears (id #2) on the resulting list? Shipped

To understand why we only get records with “matching” foreign key fields, we need to understand how the computer creates an Inner Join The computer generates a master list of all combinations of the two sets of data When you join on a specific field (foreign key), using an inner join, it will select only those combinations that contain a matching value in the join fields.

The “master list” + The resulting list will have Shipped Fruitlist The 1st combo - the Fruitid’s do not match thus it will not be included in the record set + The resulting list will have 4 x 6 or 24 possible combinations. But ONLY combinations with matching FruitID’s provide relevant information and are included in the inner join record set.

The combined record set formed from an Inner Join Fruitlist Shipped Since pears do not appear on the Shipped table it does not appear on the joined list. Similarly there is no FruitID 7 on the Fruitlist so it too will not be included on the joined list.

Now summarize this list by fruit – using the Aggregate function sum Available records The resulting dynaset:

Do we want to be able to ship a fruit (fruitid 7) that we don’t have listed on the Fruit table? Fruitlist Shipped We can control whether or not this situation can occur by editing our relationship properties.

Referential Data Integrity In Access you can choose to limit entries made in a foreign key field to those items listed in the primary key field of the related table. This is known as enforcing Referential Data Integrity. Fruitlist Received 8 1 Is Referential Data Integrity violated here?

Cascade Update Related Fields What happens when you change a FruitID? In Access, if Referential Data integrity is enforced, you can specify whether or not to Cascade Update Related records such that changing any entry in a primary key field will automatically change the entries in foreign key fields of related tables. If you change bananas to ID 9 and use this property – the related record on the shipped table will be updated FruitList 9 9 Shipped

Cascade Delete Related Fields What happens when you change a FruitID? In Access, if Referential Data integrity is enforced, you can specify whether or not to Cascade Delete Related records such that removing any entry in a primary key field will automatically remove all entries in foreign key fields of related tables. If you delete bananas’ ID 3 all records for ID3 in the payment table will be deleted Fruitlist Shipped

Fruitlist Shipped Received Given the above tables: Is data integrity enforced for each relationship? What would happen if I deleted grapes from the Fruitlist? What would happen if I changed the Fruitid of grapes to 6?

These rules are specified from the Relationships table – Edit Relationships

Can we join more than 2 tables at a time? Much like a manual process - if a piece of data cannot be uniquely associated with a corresponding value – it may not be possible to “join” them (many-many relationship). Consider some of the possible relationship scenarios (∞-1- ∞,1-∞-1) and explore how Access will join tables with these types of relationships.

Joining More than Two Tables with a many-one-many relationship 1 8 1 8 Receive FruitList Shipped Here each fruit may have multiple shipments and multiple receipts. Therefore there is a many-to-many relationship between the records on the Orders table and the records on the Payments table.

Joining More than Two Tables How does Access combine more than two tables? First it combines just two tables in the same manner we’ve seen before to create the combined record set. FruitList Shipped

Joining More than Two Tables Next Access takes this combined record set and joins it by FruitId to the Received table Received Combined Record Set Since the joins are all on the same foreign key field - the combined record set contains only those records where the join fields match in all three fruitid fields

This results in the following Record Set: Only matching records from all 3 tables are selected. So if grapes have no receipts, even if it has shipments it will not appear in the query result For records with both shipments and receipts the computer looks at every possible combination Apples with two shipments and two receipts results in 4 joins – Is this correct?

Are the resulting values correct? 8 1 1 8 Shipped Fruitist Received Group by Apples has shipped 5 and received of 7 not the values shown Directly joining all three tables that have a many-one-many relationship results in incorrect values

Can these three tables be directly joined to give us a valid record set ? 1 8 8 1 Fruitlist Vendors Received Notice the Vendor field has been added to the Received table and related to a Vendors table Does each received listing (many) correspond to a single vendor (one) and a single fruit (one)? If so – then this 3-way join is valid.

Here is the record set resulting from Joining all 3 tables having a One-Many-One Relationship Fruitlist & Received joined on Fruitid Combined records joined by Vendor Is this a valid record set – are items missing that should be included, are items duplicated?

In a One-Many-One relationship, a three-way join does result in a valid record set. 1 8 8 1 Fruitlist Vendors Received What happened to fruitid 2 (pears) or vendor 4 (California Inc)?

Can We Join More than Two Tables With Other Relationship Combinations? If a single record on the “many” side can be related to a single unique record on the other tables we combine these 3 tables to get a valid record set. 8 1 8 1 Fruitlist Type Received One can combine 3 or more tables into valid record sets as long as there are no “many” to “many” relationships resulting directly or indirectly from these joins

Valid record sets from joins of 3 or more tables can be created as long as there are no many to many (∞-1-∞) relationships 8 1 1 8 Type Received Fruitlist

Joining Tables - Recap: Inner Join of two tables (or queries) Join on a valid foreign key field - (one-one, one-many) Record set contains only records in common Referential Data Integrity Can be used to limit records entries on related tables Can also set Cascade Update and Delete Properties Inner Join of 3 or more table Record sets for joins with a many-many relationship (∞-1- ∞) will result in invalid record sets

Now consider the snacks database again Now consider the snacks database again. What type of relationships are there? Is Referential data Integrity violated for any relationship? Products: Orders: Suppliers:

Create a list of ProductID’s and SupplierName for all BA products Suppliers SupplierID SupplierName Products CategoryID ProductName Unitsinstock ProductID

When we join 2 tables a “master list” is created These are the 1st 10 elements of the master list. The total number of elements of this list will be 8*5 or 40. Only the records with matching supplier ids will be included when an inner join is executed.

Here is the resulting record set from an Inner Join on Supplierid There are 7 elements created on the master list with matching foreign keys. Since supplierid 3 has no products on our product list, it does not appear anywhere on this “joined” list.

From the joined record set – Access then selects only those records that meet the specified criterion. The resulting dynaset is: From the resulting record set – only those with a category of BA are selected

Write a query to summarize by supplier name the number of units in stock of that supplier’s products. Suppliers SupplierID SupplierName Products CategoryID ProductName Unitsinstock ProductID Does the foreign key, SupplierID, need to be on the QBE grid?

The resulting dynaset: Write a query to summarize by supplier name the number of units in stock of that supplier’s products. The resulting dynaset: Why isn’t Larsen’s Nuts listed with 0 UnitsInStock?

Does this set of joins give us a valid record set? For each order, list the product name, supplier name and quantity ordered Suppliers SupplierID SupplierName Products CategoryID ProductName Unitsinstock ProductID Orders Orderdetailid ProductId Date Sold Quantity Does this set of joins give us a valid record set?

Query result: Products: How does the violation of Referential Data Integrity affect the resulting dynaset? Orders: Suppliers: Query result: