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
36
Embed
CS&E 1111 AcInnerJoins Inner Joins Objectives: Creating Queries with data from Multiple Tables Joining two tables using an Inner Join Referential Data.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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?
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
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.
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.
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.
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 –
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.
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.
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
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
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.
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