ISP 121

9
ISP 121 Access Normalization and Relationships

description

ISP 121. Access Normalization and Relationships. Normalization. Say we’re operating a pet day-care and we need to keep information on our pets/customers. What happens when a customer has more than one pet? Create a separate table for just the pets. How do you link the two tables together?. - PowerPoint PPT Presentation

Transcript of ISP 121

Page 1: ISP 121

ISP 121

AccessNormalization and Relationships

Page 2: ISP 121

Normalization

• Say we’re operating a pet day-care and we need to keep information on our pets/customers. What happens when a customer has more than one pet?

• Create a separate table for just the pets.

• How do you link the two tables together?

Page 3: ISP 121

Normalization ExamplePet Information(originally)Customer ID (primary key)Customer Last NameCustomer First NameCustomer AddressCustomer CityCustomer StateCustomer ZipCustomer Phonethese fields repeat 1 to n times

Pet IDType of AnimalPet NameBreedColorWeightHeight

Customer TableCustomer ID (primary key)Last NameFirst NameAddressCityStateZipPhone

Pet Table

Pet IDType of AnimalPet NameBreedColorWeightHeightCustomer ID (nota primary key here!)

Primary Key in Customer Table?Foreign Key in Pet Table?Is there a primary Key in Pet Table?

Relationship

Page 4: ISP 121

Another ExampleStudent Records

Student ID (primary key)NameAddressCityStateZipPhonethese fields repeat 1 to n times

Class NameGradeNumber Credits

MajorMinorDegree Sought

Student Info

Student IDNameAddressCityStateZipPhoneMajorMinorDegree Sought

Grades

Class NameGradeNumber CreditsStudent ID

Before

After

Page 5: ISP 121

Let’s Try This One In Access

Customer IDCustomer Last NameCustomer PhoneCustomer AddressCustomer CityCustomer StateCustomer Zipfollowing fields repeat 1 to n times: Sales Transaction Date Sales Amount

Let’s first create the two tables using paper and pencil, thenwhen we know they are correct, let’s enter the tables into Access.

Page 6: ISP 121

RelationshipsRemember that relational databases split data between two or more tables. To link these tables, we use a foreign key to tie them together.

• Usually the linking field is the primary key in one database, but not a primary key in another (if it is cross-reference by another table, it is considered a foreign key).• Tables don't become magically related... you will have to relate them (or verify that Access has related them correctly).  Usually you only want to put tables in a database that you will relate in some way.• Linking fields must be the same data type.

Types of Relationships:

• One-to-one• One-to-many• Many-to-one• Many-to-many (not an option!!)

Page 7: ISP 121

Relationships

Three important thoughts about linking tables:

• You can link only tables that are in the same database.

• You can link queries to tables, but that’s unusual.

• You need to tell Access how your tables are related up front – it is a formal process.

Page 8: ISP 121

RelationshipsCreating Formal Relationships:

• Click on the Relationships button on the toolbar.

• Click on the Show Table button on the toolbar. Add tables if needed by choosing add. Click close after you are done.

• Decide which two tables you want to link. Put the mouse pointer on the field you want to link in the parent table (which will be the primary key of that table) and hold the left mouse button down.  While holding the mouse button, slide the mouse from one linking field to the other.

• Choose Enforce Referential Integrity (ensures that relationships between records in related tables are valid and the you don't accidentally delete or change related data).  Then choose create.  Repeat.

Page 9: ISP 121

Why do we have relational databases?

• Eliminates redundancy

• Makes adding data easier

• Allows for more secure access to only parts of the data