Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Inner Joins Objectives: Creating Queries with data from Multiple Tables Joining two tables using an Inner Join Referential Data Integrity Cascade Update."— Presentation transcript:

1 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

2 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

3 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

4 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?

5 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

6 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.

7 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.

8 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.

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

10 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.

11 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?

12 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

13 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

14 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?

15 These rules are specified from the Relationships table – Edit Relationships

16 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.

17 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.

18 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

19 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

20 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?

21 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

22 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.

23 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?

24 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)?

25 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

26 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

27 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

28 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:

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

30 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.

31 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.

32 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

33 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?

34 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?

35 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?

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


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

Similar presentations


Ads by Google