d1 fma review. many-to-many relationships - examples car hire –a customer may hire one or more...

13
D1 FMA Review

Upload: camron-barrett

Post on 19-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

D1 FMA Review

Page 2: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Many-to-Many Relationships - Examples

• Car Hire– A customer may hire one or more cars– A car may be hired by one or more customers

• Gym Club– A member may book one or more classes– A class may be booked by one or more

members

Page 3: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Linking Table• Breaks many-to-many relationship into two one-

to-many relationships• Car Hire – bookings table

– Each record relates to a booking made by one customer for one car.

– A customer may make one or more bookings– A car may be hired by one or more customers

• Gym Club – bookings table– Each record relates to a booking made by one

member for one class– A member may book one or more classes– A class may be booked by one or more members

Page 4: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Gym Club Model

Composite Primary Key

The combination of ClassID and MemberID is guaranteed to be unique as a member can only have one booking for each class.

What is the problem with using MemberID as the primary key for the bookings table?

Page 5: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Car Hire Model

Composite Primary Key The combination of CarID and StartDate is guaranteed to be unique as a car cannot be booked more than once on the same date.

Why is it not suitable to use CarID and Hirer as a composite Primary Key for the Hire Details table?

Page 6: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Non-key dependencies- Car Hire

Cars are classified by Type.

Cost per day relates to the type not to the car.

Split into two related tables

What is the advantage of doing this?

Page 7: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Non-key dependencies - Gym Club

Each member has a particular status

Fee and number of classes relate to the status not to the member

What are the advantages of splitting the data in this way?

Page 8: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Referential Integrity

• You must enforce referential integrity for all the relationships in your database

• Prevents a record from being deleted from the parent table if there are related records in the child table

• Requires every foreign key in the child table to relate to a primary key in the parent table.

Page 9: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Examples• A customer/member whose details are not

recorded in the customer/member table cannot make a booking for a car/class

• A customer/member cannot be deleted from the customer/member table if there are bookings recorded for that customer/member

• Bookings cannot be made for cars/classes that are not recorded in the database

• A class/car cannot be deleted if bookings exist for that class/car

Page 10: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Primary and Foreign Keys

• A foreign key in a child table must have the same data type and size as the primary key in its parent table.

• An autonumber is a numeric data type with size …?

• A foreign key relating to an autonumber primary key will have the data type …? with size ...?

• Should the foreign key be an autonumber?• Do the primary and foreign keys have to have

the same field name?

Page 11: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Using Field Properties

• Use field properties to control the data that can be entered into your tables and reduce the risk of errors

• You don’t have to set all properties for all fields

• Consider which ones are appropriate and set them accordingly

Page 12: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

How might these field properties be used?

• Field Size

• Format

• Input mask

• Caption

• Default value

• Validation rule\text

• Required

Page 13: D1 FMA Review. Many-to-Many Relationships - Examples Car Hire –A customer may hire one or more cars –A car may be hired by one or more customers Gym Club

Remember…• Tables and relationships are the

foundations of your database• You must get them right before you start to

build other objects – queries, forms, reports – based on them

• It is complicated (and a lot of work) to modify the underlying structure of the database once you have created your queries, forms and reports

• Spend the time to get this right before you go any further