cs&e 1111 acinnerjoins inner joins objectives: creating queries with data from multiple tables...
TRANSCRIPT
CS&E 1111 AcInnerJoins
Inner JoinsInner 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: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?
18
81
* Primary Keys
Fruits Fruitid Priceapples 1 1.29$ pears 2 2.45$ bananas 3 0.15$ grapes 4 1.89$
FruitidQty Shipped
4 23 44 31 27 11 3
FruitidQty Received
1 31 43 65 5
FruitidQty Received
Received
FruitidFruitPrice
FruitListFruitidQty Shipped
Shipped
FruitList Received Shipped
CS&E 1111 AcInnerJoins
Using Multiple Tables in a QueryUsing Multiple Tables in a QueryQueries 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
CS&E 1111 AcInnerJoins
Create a list of Fruit, by name/id and the quantity shipped.Create a list of Fruit, by name/id and the quantity shipped.
What does the resulting dynaset look like?
8
1Fruitid
FruitPrice
FruitListFruitidQty Shipped
Shipped
FIELD Fruit Fruitid Qty ShippedTABLE Fruitlist Fruitlist ShippedTOTALSORTSHOWCRITERIAOROR
CS&E 1111 AcInnerJoins
The “inner join” results in a list of only records which have a matching record on the related table
The “inner join” results in a list of only records which have a matching record on the related table
Fruits Fruitidapples 1 pears 2 bananas 3 grapes 4
Fruits FruitidQty Shipped
apples 1 2apples 1 3bananas 3 4grapes 4 3grapes 4 2
Joined of FruitID
What happened to the shipment for fruitID 7?
What happened to pears (id #2) on the resulting list?Shipped
Fruitlist
FruitidQty Shipped
4 23 44 31 27 11 3
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 “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.
ShippedFruitlistFruits Fruitidapples 1 pears 2 bananas 3 grapes 4
FruitidQty Shipped
4 23 44 31 27 11 3
fruitsfruitlist.fruitid
fruitship.fruitid
qty shipped
apples 1 4 2pears 2 4 2bananas 3 4 2grapes 4 4 2apples 1 3 4pears 2 3 4bananas 3 3 4grapes 4 3 4apples 1 4 3pears 2 4 3bananas 3 4 3grapes 4 4 3apples 1 1 2pears 2 1 2bananas 3 1 2grapes 4 1 2apples 1 7 1pears 2 7 1bananas 3 7 1grapes 4 7 1apples 1 1 3pears 2 1 3bananas 3 1 3grapes 4 1 3
The 1st 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
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
FruitlistFruits Fruitidapples 1 pears 2 bananas 3 grapes 4
Fruits FruitidQty Shipped
apples 1 2apples 1 3bananas 3 4grapes 4 3grapes 4 2
FruitidQty Shipped
4 23 44 31 27 11 3
CS&E 1111 AcInnerJoins
Now summarize this list by fruit – using the Aggregate function sum
Now summarize this list by fruit – using the Aggregate function sum
Available records
The resulting dynaset:
Fruits FruitidQty Shipped
apples 1 2apples 1 3bananas 3 4grapes 4 3grapes 4 2
FIELD Fruit Fruidid Qty ShippedTABLE Fruitlist Fruistlist ShippedTOTAL Group by Group by SumSORTSHOW x x xCRITERIA
Fruits FruitidSum of Quantity Shipped
apples 1 5bananas 3 4grapes 4 5
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
FruitlistFruits Fruitidapples 1 pears 2 bananas 3 grapes 4
Fruits FruitidQty Shipped
apples 1 2apples 1 3bananas 3 4grapes 4 3grapes 4 2
FruitidQty Shipped
4 23 44 31 27 11 3
CS&E 1111 AcInnerJoins
Referential Data IntegrityReferential 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?
81
Fruitlist Received
Fruits Fruitid Priceapples 1 1.29$ pears 2 2.45$ bananas 3 0.15$ grapes 4 1.89$
FruitidQty Received
1 31 43 65 5
CS&E 1111 AcInnerJoins
Cascade Update Related FieldsCascade Update Related FieldsWhat 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
FruitListFruits Fruitidapples 1 pears 2 bananas 3 grapes 4
FruitidQty Shipped
4 23 44 31 27 11 3
9
9
CS&E 1111 AcInnerJoins
Cascade Delete Related FieldsCascade Delete Related FieldsWhat 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 Fruits Fruitidapples 1 pears 2 bananas 3 grapes 4
FruitidQty Shipped
4 23 44 31 27 11 3
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 Shipped
FruitidQty Received
1 31 43 64 2
FruitidQty Shipped
4 23 44 31 27 11 3
Fruitlist
Fruits Fruitidapples 1 pears 2 bananas 3 grapes 4
CS&E 1111 AcInnerJoins
These rules are specified from the Relationships table – Edit Relationships
These rules are specified from the Relationships table – Edit Relationships
CS&E 1111 AcInnerJoins
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.
Can we join more than 2 tables at a time?Can we join more than 2 tables at a time?
CS&E 1111 AcInnerJoins
Joining More than Two Tableswith a many-one-many relationship
Joining More than Two Tableswith 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.
18 81
FruitidQty Received
1 31 43 65 5
FruitidQty Shipped
4 23 44 31 27 11 3
Fruits Fruitidapples 1 pears 2 bananas 3 grapes 4
Receive FruitListShipped
CS&E 1111 AcInnerJoins
Joining More than Two Tables 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
FruitidQty Shipped
4 23 44 31 27 11 3
fruitsfruitlist.fruitid
fruitship.fruitid
qty shipped
apples 1 1 2apples 1 1 3bananas 3 3 4grapes 4 4 3grapes 4 4 2
Fruits Fruitidapples 1 pears 2 bananas 3 grapes 4
CS&E 1111 AcInnerJoins
Joining More than Two Tables 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
ReceivedCombinedRecord Set
FruitidQty Received
1 31 43 65 5
fruitsfruitlist.f
ruitidfruitship.fruitid
qty shipped
receipts.fruitid
qty received
apples 1 1 2 1 3apples 1 1 3 1 3apples 1 1 2 1 4apples 1 1 3 1 4bananas 3 3 4 3 6
fruitsfruitlist.fruitid
fruitship.fruitid
qty shipped
apples 1 1 2apples 1 1 3bananas 3 3 4grapes 4 4 3grapes 4 4 2
CS&E 1111 AcInnerJoins
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?
This results in the following Record Set:This results in the following Record Set:
fruits fruitidqty
receivedqty
shippedapples 1 3 2apples 1 3 3apples 1 4 2apples 1 4 3bananas 3 6 4
CS&E 1111 AcInnerJoins
Are the resulting values correct? 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
FruitidQty Received
1 31 43 65 5
FruitidQty Shipped
4 23 44 31 27 11 3
Fruits Fruitidapples 1 pears 2 bananas 3 grapes 4
ReceivedFruitist
Shipped
18 81
fruitsfruiti
dSumOfqty received
SumOfqty shipped
apples 1 14 10bananas 3 6 4
fruits fruitidqty
receivedqty
shippedapples 1 3 2apples 1 3 3apples 1 4 2apples 1 4 3bananas 3 6 4
CS&E 1111 AcInnerJoins
Can these three tables be directly joined to give us a valid record set ?
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
Fruits Name Fruitid
apples 1 pears 2 bananas 3 grapes 4
FruitidQty Received Vendor
1 3 21 4 33 6 14 5 2
Vendor Company Name1 Washington Co.2 ABC Importers3 Florida Grows4 California Inc
ReceivedFruitlistVendors
8 81 1
CS&E 1111 AcInnerJoins
Here is the record set resulting from Joining all 3 tables having a One-Many-One Relationship
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?
Fruits NameFruitlist.Fruidid Fruitid
Qty Received
Received.Vendor
Vendors.Vendor Company Name
apples 1 1 3 2 2 ABC Importersapples 1 1 4 3 3 Florida Growsbananas 3 3 6 1 1 Washington Co.grapes 4 4 5 2 2 ABC Importers
FruitidQty Received Vendor
1 3 21 4 33 6 14 5 2
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.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)?
Fruits Name Fruitid
apples 1 pears 2 bananas 3 grapes 4
FruitidQty Received Vendor
1 3 21 4 33 6 14 5 2
Vendor Company Name1 Washington Co.2 ABC Importers3 Florida Grows4 California Inc
Received FruitlistVendors
8 81 1
Company Name Fruits Name
Qty Received
ABC Importers apples 3Florida Grows apples 4Washington Co. bananas 6ABC Importers grapes 5
CS&E 1111 AcInnerJoins
Can We Join More than Two Tables With Other Relationship Combinations?
Can We Join More than Two Tables With Other Relationship Combinations?
Fruits Name Fruitid type
apples 1 Apears 2 Abananas 3 Bgrapes 4 C
FruitidQty Received Vendor
1 3 21 4 33 6 14 5 2
Received Fruitlist
8 1type origin
A domesticB BrazilC Chile
18
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
Valid record sets from joins of 3 or more tables can be created as long as there are no
many to many (∞-1-∞) relationships
Fruits Name Fruitid type
apples 1 Apears 2 Abananas 3 Bgrapes 4 C
FruitidQty Received Vendor
1 3 21 4 33 6 14 5 2
Received
Fruitlist8
1
type origin
A domesticB BrazilC Chile
1
8
Type
CS&E 1111 AcInnerJoins
Joining Tables - Recap: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
CS&E 1111 AcInnerJoins
Order Detail ID
Product ID
Date Sold Quantity
1 31 4/30/97 222 40 4/30/97 553 44 4/30/97 184 40 4/30/97 20
Product NameCategory
IDSupplier
IDProduct
IDUnits In Stock
Unit Price
Granola Gems CA 2 2 10 0.37Dingers, Cupcake BA 2 14 10 0.65Cakelets, Vanilla BA 1 15 5 0.73Cookie, Chocolate BA 1 22 12 0.60Cider mix, dry BE 11 31 34 0.12Root beer BE 10 40 21 0.31Mango Refresher BE 10 44 48 0.95Cranberry Combo BE 10 45 48 1.34
Products:
Suppliers:
Now consider the snacks database again. What type of relationships are there? Is Referential data Integrity violated for any relationship?
Orders:
Supplier ID SupplierName1 Elegance Bakery2 Overholts Baking3 Larsen Nuts9 JuJu Bee Candies10 Everest Beverages
CS&E 1111 AcInnerJoins
Create a list of ProductID’s and SupplierName for all BA productsCreate a list of ProductID’s and SupplierName for all BA products
SuppliersSupplierIDSupplierName
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductID
FIELD ProductID SupplierName CategoryTABLE Products Suppliers ProductsTOTALSORTSHOWCRITERIA "BA"OROR
CS&E 1111 AcInnerJoins
When we join 2 tables a “master list” is createdWhen 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.
Product NameCategory
ID
Products.Supplier
IDProduct
IDUnits In Stock Unit Price
Suppliers.SupplierID SupplierName
Granola Gems CA 2 2 10 0.37 1 Elegance Bakery
Granola Gems CA 2 2 10 0.37 2 Overholts Baking
Granola Gems CA 2 2 10 0.37 3 Larsen Nuts
Granola Gems CA 2 2 10 0.37 9 JuJu Bee Candies
Granola Gems CA 2 2 10 0.37 10Everest Beverages
Dingers, Cupcake BA 2 14 10 0.65 1 Elegance Bakery
Dingers, Cupcake BA 2 14 10 0.65 2 Overholts Baking
Dingers, Cupcake BA 2 14 10 0.65 3 Larsen Nuts
Dingers, Cupcake BA 2 14 10 0.65 9 JuJu Bee Candies
Dingers, Cupcake BA 2 14 10 0.65 10Everest Beverages
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
Here is the resulting record set from an Inner Join on Supplierid
Product NameCategory
ID
Products.Supplier
IDProduct
IDUnits In Stock
Unit Price
Suppliers.SupplierID SupplierName
Cakelets, Vanilla BA 1 15 5 0.73 1 Elegance Bakery
Cookie, Chocolate BA 1 22 12 0.60 1 Elegance Bakery
Granola Gems CA 2 2 10 0.37 2 Overholts Baking
Dingers, Cupcake BA 2 14 10 0.65 2 Overholts Baking
Root beer BE 10 40 21 0.31 10 Everest Beverages
Mango Refresher BE 10 44 48 0.95 10 Everest Beverages
Cranberry Combo BE 10 45 48 1.34 10 Everest Beverages
CS&E 1111 AcInnerJoins
From the joined record set – Access then selects only those records that meet the specified criterion. The resulting dynaset is:
Product Name Category ID
Products.Supplier
IDProduct
IDUnits In Stock Unit Price
Suppliers.SupplierID SupplierName
Cakelets, Vanilla BA 1 15 5 0.73 1 Elegance Bakery
Cookie, Chocolate BA 1 22 12 0.60 1 Elegance Bakery
Granola Gems CA 2 2 10 0.37 2 Overholts Baking
Dingers, Cupcake BA 2 14 10 0.65 2 Overholts Baking
Root beer BE 10 40 21 0.31 10 Everest Beverages
Mango Refresher BE 10 44 48 0.95 10 Everest Beverages
Cranberry Combo BE 10 45 48 1.34 10 Everest Beverages
Product ID SupplierName
15 Elegance Bakery
22 Elegance Bakery
14 Overholts Baking
From the resulting record set – only
those with a category of BA are
selected
CS&E 1111 AcInnerJoins
FIELD SupplierName UnitsInStockTABLE Suppliers ProductsTOTAL Group by SumSORTSHOWCRITERIAOR
Write a query to summarize by supplier name the number of units in stock of that supplier’s products. Write a query to summarize by supplier name the number of units in stock of that supplier’s products.
SuppliersSupplierIDSupplierName
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductID
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?
SupplierName SumOfUnitsInStock
Elegance Bakery 17
Everest Beverages 117
Overholts Baking 20
Write a query to summarize by supplier name the number of units in stock of that supplier’s products. 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 For each order, list the product name, supplier name and quantity ordered
FIELD ProductName SupplierName QuantityTABLE Products Suppliers OrdersTOTALSORTSHOWCRITERIAOROR
SuppliersSupplierIDSupplierName
ProductsCategoryIDProductNameUnitsinstockSupplierIDProductID
OrdersOrderdetailidProductIdDate SoldQuantity
Does this set of joins give us a valid record set?
CS&E 1111 AcInnerJoins
Product Name SupplierName Quantity
Root beer Everest Beverages 55
Mango Refresher Everest Beverages 18
Granola Gems Overholts Baking 20
Products:
Order Detail ID
Product ID Date Sold Quantity
1 31 4/30/97 222 40 4/30/97 553 44 4/30/97 184 2 4/30/97 20
Product NameCategory
IDSupplier
IDProduct
IDUnits In Stock
Unit Price
Granola Gems CA 2 2 10 0.37Dingers, Cupcake BA 2 14 10 0.65Cakelets, Vanilla BA 1 15 5 0.73Cookie, Chocolate BA 1 22 12 0.60Cider mix, dry BE 11 31 34 0.12Root beer BE 10 40 21 0.31Mango Refresher BE 10 44 48 0.95Cranberry Combo BE 10 45 48 1.34
Suppliers: Orders:
Supplier ID SupplierName1 Elegance Bakery2 Overholts Baking3 Larsen Nuts9 JuJu Bee Candies10 Everest Beverages
How does the violation of Referential Data Integrity affect the resulting dynaset?
Query result: