CS&E 1111 AcInnerJoins Inner Joins Objectives: Creating Queries with data from Multiple Tables Joining two tables using an Inner Join Referential Data.

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.
Lesson 32: Designing a Relational Database. 2 Lesson Objectives After studying this lesson, you will be able to:  Identify and apply principles for good.
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.
Concepts of Database Management, Fifth Edition
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.
Relational Database Concepts. Let’s start with a simple example of a database application Assume that you want to keep track of your clients’ names, addresses,
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!
Computer Science & Engineering 2111 Lecture 11 Querying a Database 1.
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.
Copyright 2007, Paradigm Publishing Inc. ACCESS 2007 Chapter 2 BACKNEXTEND 2-1 LINKS TO OBJECTIVES Creating Related Tables Creating Related Tables Determining.
Exploring Microsoft Access Chapter 6 Many-to-Many Relationships: A More Complex System.
Introduction to Database using Microsoft Access 2013 Part 7 November 19, 2014.
What have we learned?. What is a database? An organized collection of related data.
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.
1 CSE 2337 Introduction to Data Management Access Book – Ch 1.
Microsoft Access 2010 Chapter 3 Maintaining a Database.
Pasewark & Pasewark 1 Access Lesson 3 Creating Queries Microsoft Office 2007: Introductory.
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.
CHAPTER 1 – INTRODUCTION TO ACCESS Aliya Farheen March 5, 2014.
Pasewark & Pasewark Microsoft Office 2003: Introductory 1 INTRODUCTORY MICROSOFT ACCESS Lesson 4 – Finding and Ordering Data.
XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database.
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.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
AOIT Database Design Unit 3, Lesson 9 Data Integrity Copyright © 2009–2011 National Academy Foundation. All rights reserved.
Writing Simple Queries in Access
Tables & Relationships
Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations.
Inner Joins Objectives: Creating Queries with data from Multiple Tables Joining two tables using an Inner Join Referential Data Integrity Cascade Update.
Objectives Query for top values Create a parameter query
CIS 155 Table Relationship
Introduction to Database Systems
Tutorial 3 – Querying a Database
Relational Queries (query 12) Display vendor contact info (contact person and phone number) for inventory products (relationship query) Query: Inventory.
Relationships While we are on the subject of Relationships, let’s take a quick look at them.
Presentation transcript:

CS&E 1111 AcInnerJoins 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

CS&E 1111 AcInnerJoins Let’s look at a database with 3 tables: l What are the primary keys in these tables? l What are the foreign key relationships we can setup? * Primary Keys Fruitid Qty Received Received Fruitid Fruit Price FruitList Fruitid Qty Shipped Shipped FruitListReceivedShipped

CS&E 1111 AcInnerJoins 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: l Add both tables to the query design view l Setup the appropriate relationships if they have not been done so on the relationship table. l Choose the appropriate fields, criteria, sort, etc. l Run your query and look at the results

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

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

CS&E 1111 AcInnerJoins 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. To understand why we only get records with “matching” foreign key fields, we need to understand how the computer creates an Inner Join

CS&E 1111 AcInnerJoins The “master list” + 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. Shipped Fruitlist The 1 st combo - the Fruitid’s do not match thus it will not be included in the record set

CS&E 1111 AcInnerJoins The combined record set formed from an Inner Join 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. Shipped Fruitlist

CS&E 1111 AcInnerJoins Now summarize this list by fruit – using the Aggregate function sum Available records The resulting dynaset :

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

CS&E 1111 AcInnerJoins 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. Is Referential Data Integrity violated here? 8 1 FruitlistReceived

CS&E 1111 AcInnerJoins 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 Shipped FruitList 9 9

CS&E 1111 AcInnerJoins 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 Shipped Fruitlist

CS&E 1111 AcInnerJoins 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? Received ShippedFruitlist

CS&E 1111 AcInnerJoins These rules are specified from the Relationships table – Edit Relationships

CS&E 1111 AcInnerJoins l 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). l Consider some of the possible relationship scenarios (∞-1- ∞,1-∞-1) and explore how Access will join tables with these types of relationships. Can we join more than 2 tables at a time?

CS&E 1111 AcInnerJoins Joining More than Two Tables with a many-one-many relationship 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 Receive FruitList Shipped

CS&E 1111 AcInnerJoins 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. Shipped FruitList

CS&E 1111 AcInnerJoins Joining More than Two Tables Next Access takes this combined record set and joins it by FruitId to the Received table 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 Received Combined Record Set

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

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

CS&E 1111 AcInnerJoins Can these three tables be directly joined to give us a valid record set ? 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. Notice the Vendor field has been added to the Received table and related to a Vendors table Received Fruitlist Vendors

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

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

CS&E 1111 AcInnerJoins Can We Join More than Two Tables With Other Relationship Combinations? Received Fruitlist 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. 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 Type

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

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

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

CS&E 1111 AcInnerJoins Create a list of ProductID’s and SupplierName for all BA products Suppliers SupplierID SupplierName Products CategoryID ProductName Unitsinstock SupplierID ProductID

CS&E 1111 AcInnerJoins When we join 2 tables a “master list” is created These are the 1 st 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.

CS&E 1111 AcInnerJoins 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. Here is the resulting record set from an Inner Join on Supplierid

CS&E 1111 AcInnerJoins 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

CS&E 1111 AcInnerJoins 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 SupplierID ProductID Does the foreign key, SupplierID, need to be on the QBE grid?

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

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

CS&E 1111 AcInnerJoins Products: Suppliers: Orders: How does the violation of Referential Data Integrity affect the resulting dynaset? Query result: