Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

2 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? 1 8 8 1 * Primary Keys Fruitid Qty Received Received Fruitid Fruit Price FruitList Fruitid Qty Shipped Shipped FruitListReceivedShipped

3 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

4 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

5 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

6 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

7 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

8 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

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

10 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

11 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

12 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

13 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

14 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

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

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

17 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. 1 88 1 Receive FruitList Shipped

18 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

19 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

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

21 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 1 88 1

22 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 8 8 1 1

23 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

24 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 8 8 1 1

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

26 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 8 1 1 8 Type

27 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

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

29 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

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

31 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

32 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

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

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

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

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


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

Similar presentations


Ads by Google