Reldes 00
Reldes 00
Relational Design
Contents
OVERVIEW..........................................................................................................
Overview
Microsoft® Access provides a number of tools that you can use to create a
relational database even if you don't have much experience with relational
design. For example, you can use the Database Wizard to create ten
predefined types of databases, from an Asset Tracking database to a Time
and Billing database.
Or, if you already have your data in a spreadsheet or table of some sort, but
you have repeating data (unnormalized data) and you want to separate the
data out into two or more relational Microsoft Access tables, you can use
the Table Analyzer Wizard to help you decide which fields need to be
moved to separate tables.
If you're not satisfied with having Microsoft Access do the work for you,
however, and you want to know more about relational database design, this
document is for you. It shows you how to plan and to design a database
from the ground up. For practical examples, it uses the database design of
the Northwind Traders sample database included in the Microsoft Access
package.
When you use your data, you then combine and present facts in many
different ways. For example, you may print reports that combine facts about
employees and facts about sales.
When you design a database, you first break down the information you want
to keep as separate subjects, and then you tell Microsoft Access how the
subjects are related to each other so that Microsoft Access can bring the
right information together when you need it.
Step One: Determine the purpose of your database. This will help you
decide which facts you want Microsoft Access to store.
Step Two: Determine the tables you need. Once you have a clear
purpose for your database, you can divide your information into separate
subjects, such as "Employees" or "Orders." Each subject will be a table in
your database.
Step Three: Determine the fields you need. Decide what information you
want to keep in each table. Each category of information in a table is called
a field and is displayed as a column in the table. For example, one field in
an Employees table could be Last Name; another could be Hire Date.
Step Four: Determine the relationships. Look at each table and decide
how the data in one table is related to the data in other tables. Add fields to
tables or create new tables to clarify the relationships, as necessary.
Step Five: Refine your design. Analyze your design for errors. Create the
tables and add a few records of sample data. See if you can get the results
you want from your tables. Make adjustments to the design as needed.
Don't worry if you make mistakes or leave things out of your initial design.
Think of it as a rough draft that you can refine later. Experiment with sample
data and prototypes of your forms and reports. With Microsoft Access, it's
easy to change the design of your database as you're creating it. However,
it becomes much more difficult to make changes to tables after they're filled
with data and after you've built forms and reports. For this reason, make
sure that you have a sound design before pushing too far ahead.
Talk to the people who will use the database. Brainstorm about the
questions you'd like the database to answer. Sketch out the reports you'd
like it to produce. Gather the forms you currently use to record your data.
You'll use all this information in the remaining steps of the design process.
Next, gather all the forms and reports that contain information the database
should be able to produce. Northwind Traders currently uses a printed
report to keep track of products being ordered and an order form to take
new orders. The following illustration shows these two documents.
Northwind Traders also needs to print mailing labels for customers,
employees, and suppliers.
After gathering this information, you're ready for the next step.
See the preceding order form as an example. It includes facts about the
customer -- the customer's address and phone number -- along with facts
about the order. This form provides you with a number of facts that you
know you want to store in your database. But you'd run into problems if you
stored the customer facts in the same table as the order facts:
• Introducing errors in duplicate information. Suppose that one
customer places three different orders. You could add the customer's
address and phone number to your database three times, once for each
order. But this multiplies the chance of data entry errors.
Look at the information you want to get out of your database and divide it
into fundamental subjects you want to track, such as customers,
employees, products you sell, services you provide, and so on. Each of
these subjects is a candidate for a separate table.
Note: One strategy for dividing information into tables is to look at
individual facts and determine what each fact is actually about.
For example, on the Northwind Traders order form, the customer
address isn't about the sale; it's about the customer. This
suggests that you need a separate table for customers. In the
Products On Order report, the supplier's phone number isn't about
the product in stock; it's about the supplier. This suggests that
you need a separate table for suppliers.
The Northwind Traders order form and Products On Order report include
information about these subjects:
• Customers
• Suppliers
• Products
• Orders
From this list, you can come up with a rough draft of the tables in the
database and some of the fields for each table. It’s a good idea as you start
to design a database to write down the information you will need to have as
you create your tables.
Although the finished Northwind database contains other tables, this list is a
good start. Later in this paper, you'll see how to add other tables to refine
your design.
Northwind Traders sells imported specialty foods from around the world.
The employees use a Products On Order report to keep track of products
being ordered.
The report indicates that the Products table, which contains facts about
products sold, needs to include fields for the product name, units in stock,
and units on order, among others. But what about fields for the supplier
name and phone number? To produce the report, Microsoft Access needs
to know which supplier goes with each product.
One approach would be to include Supplier Name and Supplier Phone fields
in the Products table, but this can cause more problems than it solves.
Since Northwind Traders might buy many products from the same supplier,
the name and phone number of the supplier would have to be repeated in
the Products table many times. If the phone number ever changed, it would
have to be changed many times as well.
Instead, create a Suppliers table, with separate fields for the supplier's
name and phone number. In the next step, you'll add a field to the Products
table that identifies the supplier information you need.
If you already have a unique identifier for a table, such as a set of product
numbers you've developed to identify the items in your stock, you can use
that identifier as the table's primary key. But make sure the values in this
field will always be different for each record -- Microsoft Access doesn't
allow duplicate values in a primary key field. For example, don't use
people's names as a primary key, because names aren't unique. You could
easily have two people with the same name in the same table.
If you don't already have a unique identifier in mind for a table, you can use
a field that simply numbers the records consecutively. Microsoft Access can
even set up a primary key like that for you. For more information about
Primary Keys, search for "Primary Key" in Microsoft Access Help.
For example, suppose that you want to phone an employee with questions
about a sale the employee made. Employee phone numbers are recorded in
the Employees table; sales are recorded in the Orders table. When you tell
Microsoft Access which sale you're interested in, Microsoft Access can look
up the phone number based on the relationship between the two tables. It
works because Employee ID, the primary key for the Employees table, is
also a field in the Orders table. In database terminology, the Employee ID
field in the Orders table is called a foreign key, because it's a primary key
from a different table.
Note If you open the Orders table in the sample Northwind
database included with Microsoft Access in Datasheet view, you’ll
notice that there is an Employees field but no Employee ID field.
The Employees field is actually the Employee ID field, but the
Caption property is set to “Employee” and properties have been
set on the Lookup tab to display the employee names rather than
the ID numbers. The illustration above uses the Employee ID field
to make the association between the tables clear. Later
illustrations in this document also show the ID fields for education
purposes rather than the lookup text that appears in the
Northwind database. To learn more about how lookup tables
work, search for “lookup table” in Microsoft Access Help.
For example, the Suppliers and Products tables in the Northwind database
have a one-to-many relationship.
To set up the relationship, you add the field or fields that make up the
primary key on the "one" side of the relationship to the table on the "many"
side of the relationship. In this case, you would add the Supplier ID field
from the Suppliers table to the Products table, because one supplier
supplies many products. Microsoft Access uses the supplier ID number to
locate the correct supplier for each product.
The subjects of the two tables -- orders and products -- have a many-to-
many relationship. This presents a problem in database design. To
understand the problem, imagine what would happen if you tried to set up
the relationship between the two tables by adding the Product ID field to the
Orders table. To have more than one product per order, you need more
than one record in the Orders table per order. You'd be repeating order
information over and over for each record that relates to a single order -- an
inefficient design that could lead to inaccurate data. You run into the same
problem if you put the Order ID field in the Products table -- you'd have
more than one record in the Products table for each product. How do you
solve this problem?
The answer is to create a third table that breaks down the many-to-many
relationship into two one-to-many relationships. You put the primary key
from each of the two tables into the third table.
Each record in the Order Details table represents one line item on an order.
The Order Details table's primary key consists of two fields -- the foreign
keys from the Orders and Products tables. The Order ID field alone doesn't
work as the primary key for this table, because one order can have many
line items. The Order ID is repeated for each line item on an order, so the
field doesn't contain unique values. The Product ID field alone doesn't work
either, because one product can appear on many different orders. But
together the two fields always produce a unique value for each record.
In the Northwind database, the Orders table and the Products table aren't
related to each other directly. Instead, they are related indirectly through the
Order Details table. The many-to-many relationship between orders and
products is represented in the database using two one-to-many
relationships:
• The Orders and Order Detail tables have a one-to-many relationship.
Each order can have more than one line item, but each line item is
connected to only one order.
• The Products and Order Detail tables have a one-to-many relationship.
Each product can have many line items associated with it, but each line
item refers to only one product.
You could add all the fields from the Ping-Pong Players table to the
Employees table. But the Ping-Pong Players table tracks a one-time event,
and you won't need the information after the event is over. Additionally, not
all employees play ping-pong, so if these fields were in the Employees table,
they would be empty for many records. For these reasons, it makes sense
to create a separate table.
When you detect the need for a one-to-one relationship in your database,
consider whether you can put the information together in one table. If you
don't want to do that for some reason, here's how to set up the relationship:
• If the two tables have the same subject, you can probably set up the
relationship by using the same primary key field in both tables.
• If the two tables have different subjects with different primary keys,
choose one of the tables (either one) and put its primary key field in the
other table as a foreign key.
Create your tables, specify relationships between the tables, and enter a
few records of data in each table. See if you can use the database to get
the answers you want. Create rough drafts of your forms and reports and
see if they show the data you expect. Look for unnecessary duplications of
data and eliminate them.
As you try out your initial database, you will probably discover room for
improvement. Here are a few things to check for:
• Did you forget any fields? Is there information that you need that isn't
included? If so, does it belong in the existing tables? If it's information
about something else, you may need to create another table.
• Did you choose a good primary key for each table? If you use it to
search for specific records, is it easy to remember and type? Make sure
that you won't need to enter a value in a primary key field that duplicates
another value in the field.
• Are you repeatedly entering duplicate information in one of your tables?
If so, you probably need to divide the table into two tables with a one-to-
many relationship.
• Do you have tables with many fields, a limited number of records, and
many empty fields in individual records? If so, think about redesigning
the table so it has fewer fields and more records.
Example: Refining the Products Table
Each product in the Northwind Traders stock falls under a general category,
such as Beverages, Condiments, or Seafood. The Products table could
include a field that shows the category of each product.
Pascal, Fabian. SQL and Relational Basics. Redwood City: M&T Books,
1990.