330 likes | 485 Views
Computer Science & Engineering 2111. Lecture 13 Outer Joins. Research Papers Database. Primary Keys. Foreign Keys. Inner Join between Client and Payments. Notice that only records with matching values in the foreign key fields of the related tables are included in the resulting dynaset.
E N D
Computer Science & Engineering 2111 Lecture 13 Outer Joins CSE 2111 Outer Joins
Research Papers Database Primary Keys Foreign Keys CSE 2111 Outer Joins
Inner Join between Client and Payments Notice that only records with matching values in the foreign key fields of the related tables are included in the resulting dynaset Resulting Dynaset FK PK CSE 2111 Outer Joins
Outer Join between Client and Payments Outer join relative to the Client table (the primary key side of the relationship) Resulting Dynaset CSE 2111 Outer Joins
Outer Join between Client and Payments Outer join relative to the Payments table (the many side of the relationship) Notice the results look very similar to an inner join between the two tables with one major exception. Can you identify the exception? Resulting Dynaset CSE 2111 Outer Joins
Using 3 or more tables in a query Let’s look at a Many-One-Many Relationship and see what happens If I needed to obtain records from my database that included the customer information (name and ID), total charges, total payments and balance due, I would need to run a query using these tables. What would happen? CSE 2111 Outer Joins
Running a Query with Client, Charges and Payments: The Design View CSE 2111 Outer Joins
First-what should the results look like? Client Resulting Dynaset Charges Payments CSE 2111 Outer Joins
Now let’s see what really happens….. • Should Nancy have total charges of $750 and total payments of $700? • Where are the other clients? Karen Day has charges but no payments. Shouldn’t you want to see her in your results? Are there more people like this missing? What we WANT What we actually get CSE 2111 Outer Joins
So what happened? Clients Intermediate Dynaset 1 Intermediate Dynaset 2 Charges Payments Aggregate functions applied/Expressions calculated Final Dynaset CSE 2111 Outer Joins
Client Resulting Intermediate Dynaset 1 (Partial View) Charges CSE 2111 Outer Joins
Intermediate Dynaset 1 (Partial View) Payments ? Resulting Intermediate Dynaset 2 CSE 2111 Outer Joins
Aggregate functions & expressions are applied last: Resulting Intermediate Dynaset 2 Final Dynaset CSE 2111 Outer Joins
2 Major Problems exist with our results • Not all of the Clients show up in the final dynaset. • We used an Inner Join and got only those clients who are in both the Charges table and the Payments table. • Any ClientID that is not in all 3 tables will be left out of our final results. • For some of our clients, their charges and payments are wrong, resulting in an incorrect balance! CSE 2111 Outer Joins
Would it help to use an Outer Join? Actually it did- but not enough. It solved the problem of not including everyone unless they were in all 3 tables. So now we see all clients, but some of them still have wrong values for charges and payments. Notice some of the values are correct making this a very dangerous problem. If you only spot check a few values, you might not see the problem. CSE 2111 Outer Joins
Good news and Bad News • Good News: Access doesn’t always mess up queries with 3 or more tables in it. • We can predict and avoid this problem! • Bad News: You have to know what to look for to prevent this kind of problem from happening. CSE 2111 Outer Joins
Using 3 or more tables in a query Let’s look at a One-Many-One Relationship and see what happens If I needed to obtain records from my database that included the customer information (name and ID), and the Method Type, I would need to run a query using these tables. What would happen? CSE 2111 Outer Joins
Now let’s see what really happens….. What we WANT What we get CSE 2111 Outer Joins
Client Payments Intermediate Dynaset 1 CSE 2111 Outer Joins
Intermediate Dynaset 1 PaymentMethod Final Dynaset CSE 2111 Outer Joins
So what’s the difference? Many-One-Many NOT VALID!! One-Many-One OK! CSE 2111 Outer Joins
So what do we do? Split up the relationship! Client 1 Client 1 ∞ ∞ Charges Payments SUMMARIZE CHARGES BY CLIENT SUMMARIZE PAYMENTS BY CLIENT CSE 2111 Outer Joins
PaymentsByClient Tables: Client, Payments Join Type: Outer Join On: ClientID CSE 2111 Outer Joins
ChargesByClient Tables: Client, Charges Join On: ClientID Join Type: Outer CSE 2111 Outer Joins
ChargesByClient PaymentsByClient Notice that each client is listed exactly once in both queries. 1 1 ChargesByClient PaymentsByClient Join on ClientID Now we can put the relationship back together! CSE 2111 Outer Joins
Now put the two summaries together & calculate the balance due… BalanceDue Tables: PaymentsByClient,ChargesByClient Join Type: Inner Join On: ClientID Balance: [Charges]![SumOfAmount] – [Payments]![SumOfAmount] CSE 2111 Outer Joins
Are we there yet? Not quite….notice that Karen Day was charged $100 but hasn’t made a payment. Her balance should be $100 - $0 = $100, but it’s blank. Why? CSE 2111 Outer Joins
Client Payments ? Let’s take a closer look at the PaymentsByClient Query….in an outer join with respect to Clients, when a record from Clients doesn’t have any matching records in Payments, it’s included in the results, but the fields that would have come from Payments are NULL. CSE 2111 Outer Joins
Access doesn’t know what $100 – NULL is, so it punts and returns NULL as the result. But we know that in this case, NULL should be treated like zero – can we help Access out? CSE 2111 Outer Joins
NZ Function Syntax: Nz(variant, value_if_null) If this argument evaluates to NULL…. Return this value If the variant argument does NOT evaluate to NULL, Nz will return whatever the variant argument does evaluate to. CSE 2111 Outer Joins
Balance Due with the Nz function….. BalanceDue Tables: PaymentsByClient,ChargesByClient Join Type: Inner Join On: ClientID Balance: Nz([Charges]![SumOfAmount],0) – Nz([Payments]![SumOfAmount],0) CSE 2111 Outer Joins
Finally! CSE 2111 Outer Joins
Summary • Inner joins include only those combined records where the primary & foreign keys match. • Outer joins include all records from one of the tables, even if there isn’t a matching record in the other table. • Many-one-many relationships are not valid and must be broken down into multiple valid (one-many) relationships. • Use Nz to replace NULL values with zeroes. CSE 2111 Outer Joins