Creating Database Relationships

advertisement
DATABASE RELATIONSHIPS
Relationships ‘connect’ tables. In other words they link data in one table to data in another. Data
can be lined via 3 types of relationship – one-to-one, one-to-many and many-to-many. In this way
we can join many tables and extract data easily from multiple tables.
One-to-one
Each record in one table has at MOST one related record in another table. This type of relationship
is rare
One-to-many
This is the most common type. Here one record in one table can be related to many records in
another table. For example in a Purchase Order system each Customer many place many Orders but
any single Order belongs to only one Customer
Many to Many
In this type of relationship there can be many records in one table related to many records in
another table. This type of relationship cannot be represented in a relational database and must be
broken down as shown below.

The representation of 1:M and M:M relationships as multiple tables with linking fields

We are going to consider a small ordering system where the following many to many
relationship occurs:
Orders
Ordered
Customer
Product
(one customer can order many products - one product can be order by many customers)
The diagram below shows how this M:M relationship can be represented by three tables - one each
for the two entities (tblCustomer and tblProduct) and a link table (tblOrder).
Una Dooney
Page 1
Database Relationships
Exercise:
To verify this, try [mentally] answering the following questions:
1. Which Products have been ordered by Ms Castro
2. Who has ordered a Sporty Picture
3. How many items has Mr. Moos ordered and what is the total cost of those products.
Primary Key
This is the field that uniquely identifies a record in a table. Its value must be unique for each
record
Foreign Key
This is a field in one table that is linked by a relationship with the Primary Key of another table. If
we create a relationship between EmployeeID in the Employees table and EmployeeID in the Orders
table then EmployeeID is the Foreign key in the Orders table while it is the Primary key in the
Employees table
CREATING RELATIONSHIPS
1. We need to specify the relationships (links) between our table to
Access. We can do this from the main database window by pressing the relationships button
in the toolbar or you can select Tools/ Relationships from the menu bar
2. Add the three tables; tblCustomers, tblOrder, tblProduct.
3. If you cannot see the tables dialog below to Add the tables click on Relationships/Show
Table as below
Una Dooney
Page 2
Database Relationships
4. In the Relationships window select CustomerID in tblCustomer and drag it over to
CustomerId in tblOrder. The following dialog will appear asking us to configure the
relationship:
5. Select Enforce Referential Integrity and then select One to Many (Access will probably
already have guessed!) and check both Cascade options. Finally click Create.
6. The relationship window will change as follows:
WHEN CREATING
RELATIONSHIPS
ALWAYS DRAG FROM
THE ONE SIDE OF
THE RELATIONSHIP
TO THE
MANY SIDE
7. Note the ends of the relationship are labeled with:
[the one side] - a customerID can appear only once in tblCustomers
[the many side] - a customerID can appear many times in tblOrder
8. Create a relationship to join tblProduct and tblOrder.
9. Close the relationship window down, saving changes.
A LITTLE MORE ABOUT RELATIONSHIPS
Imagine a small database we've created for the Acme Widget Company. We want to track both our
employees and our customer orders. We might use a table structure similar to the one shown
below:
Una Dooney
Page 3
Database Relationships

Notice that each order is associated with a specific employee.

This information overlap presents the perfect situation for the use of a database
relationship.
Together we'll create a Foriegn Key relationship that instructs the database that the EmployeeID
column in the Orders table corresponds to the EmployeeID column in the Employees table.
Referential Integrity. In most circumstances, you will want to select this option by placing a tick in
the box provided. This is the real power of a relationship. For example it will ensure that we
cannot have an Order from a Customer, whose CustomerID does not exist in the Customer table. By
enforcing referential integrity we can ensure that the data in our database is valid and that we do
not have orders in the database for non-existent customers
Cascade You'll also notice two other options here. The "Cascade Update Related Fields" option
ensures that if an EmployeeID changes in the Employees table that change is propagated to all
related records in the Orders table.
Similarly, the "Cascade Delete Related Records" option removes all related Orders records when an
Employee record is removed. The use of these options will depend upon the particular
requirements of your database. In this example we will tick both options. You do need to be
careful about the delete option!
Join Type You can also select a Joint Type for the relationship. Join types do not affect the
relationship but effect the results of queries on the related tables
Una Dooney
Page 4
Database Relationships
Download