d1 fma review. many-to-many relationships - examples car hire –a customer may hire one or more...
TRANSCRIPT
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– A member may book one or more classes– A class may be booked by one or more
members
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
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?
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?
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?
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?
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.
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
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?
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
How might these field properties be used?
• Field Size
• Format
• Input mask
• Caption
• Default value
• Validation rule\text
• Required
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