20

One Customer has Many Pets; One Pet as One Customer, so Customer ID goes on Pet Table. One Pet has many Visits, but only one Visit refers

Embed Size (px)

Citation preview

One Customer has Many Pets; One Pet as One Customer, so

Customer ID goes on Pet Table.

One Pet has many Visits, but only one Visit refers to one Pet, so

ONE’s ID goes on

MANY’s Table.

PetID goes on Visit Table.

One VISIT Can have MANY Procedures; the table as shown above limits the visit to having three. That would be OK—until you need more. If this refers to a specific procedure which could be described by another table, it is a one-to-many relationship and can have the VisitID as Foreign Key on the Procedure Table.

No FK here CustID as FK

PetIDAsFK

VisitID asFK

Does CustomerNumber belong on the Product Table? NO

It was not on the Product Table.

Note the Order Detail Table with TWO Primary Keys.

When one ORDER could have many PRODUCTS

and one PRODUCT can be on many Orders,

this is a Many-to-Many Relationship.

Note the Order Detail Table with TWO Primary Keys.

When one ORDER (ORDER TABLE) could have many PRODUCTS

and one PRODUCT (PRODUCT TABLE) can be on many Orders,

this is a Many-to-Many Relationship.

Join TypesType 1 Join – INNER JOIN

Type 2 – LEFT OUTER JOIN

Type 3 – RIGHT OUTER JOIN

ABDEFH

BCDEFG

ABDEFH

BCDEFG

ABDEFH

BCDEFG

RESULTS of these JOINS

BDEF

ABDEFH

BCDEFG

Only fields where both tables match

All fields of left table and only matching fields from right table

All fields of right table and only matching fields from left table

Join Types from Chapter 9

Type 1 Join –

INNER JOIN

Type 2 –

LEFT OUTER JOINType 3 –

RIGHT OUTER JOIN

ABDEFH

BCDEFG

ABDEFH

BCDEFG

ABDEFH

BCDEFG

RESULTS of these JOINS

BDEF

ABDEFH

BCDEFG

Only fields where both tables match

All fields of left table and only matching fields from right table

All fields of right table and only matching fields from left table

One-to-One Relationships exist when ONE item from the Primary Table will match only ONE item on the Secondary Table and ONE item in the Secondary Table will match only ONE item in the Primary Table.

An example is Business Address and Mailing Address. One business will have mail either go to that same address (actual physical location is used for mailing) or sometimes to another address, such as a P. O. Box. This can be “handled” by a One-to-One relationship called an Entity Subtype. We’ll learn how to choose one rather than the other later by using Conditional IIF Statements. However, if you leave the default join type, INNER JOIN, you won’t have much data! Using this (where not all businesses use a P. O. Box for mail) requires us to use an Outer Join.

If we have 100 businesses and only 20 of them have entries in this one-to-one relationship table and we have an “INNER JOIN,” which is what you get if you don’t SPECIFY it yourself, you’ll only see 20 businesses in your results! If you only mail out your flyers to that group, you have lost a lot of business!

One-to-One Relationships

One-to-One Lookup One-to-Many

Many-to-Many

Details table with composite key

Relationships

What do we need to do.doc