0144 Access Database Design Course Tutorial
0144 Access Database Design Course Tutorial
http://oit.wvu.edu/training/classmat/db/
Thank you,
The OIT Technical Support Services Trainers
West Virginia University
Data Organization
There is a hierarchy of components which constitute the collection of data maintained
by any DBMS:
Database
Record
Field
Value
A database is the entire file or collection of files maintained as a unit by the DBMS.
The University, like most large organizations, has several databases used to manage its
operations: a human resources database of all employees, an inventory database of the
organization’s physical assets, a financial database of receipts and expenditures, and a
student database containing information about students and the courses they take,
among others. Individual departments may have their own databases for different
purposes, just as individual faculty may have databases for information such as
bibliographies of useful works in the faculty member’s field or data collected during an
experiment.
The database is composed of records, which are structured collections of closely
related data. The nature of the relationship among the data in a record will depend on
the purpose of the database. In a human resources database, each record will contain
information about one employee. In a bibliographic database, a record would
correspond to a book, journal article, or other written work.
The structure of a record is determined by the set of fields from which it is composed.
Each field is a place where data with a particular meaning is kept. A human resources
database record would include fields for name, address, social security number, date of
employment, salary, and other information. A bibliographic database record would
include fields for a work’s title, authors, publisher, date of publication, and other
information.
The content of each field in a record is its value—the specific text, number, date, or
other information stored in that field of that record.
There are different types of databases, but we will be looking only at relational
databases, since that is the type of database managed by Access. In a relational
database, there is an additional level in the hierarchy of data organization:
Table Design
An important part of designing a relational database, including Access databases, is
determining what tables will be used to organize the data.
Consider the simple case of a class list and suppose that we wish to keep track of the
class’s name, number, and department; the name of the teacher; and the teacher’s
office number. We could include all of the information in one table, as shown below.
Class_Name Class_No Class_Teacher Class_Dept Class_Office
Calculus 200 Mr. Brown Mathematics G105
Interior Design 304 Ms Smith Design G103
Algebra 101 Mr. Brown Mathematics G105
Geometry 110 Mr. Jones Mathematics G107
However, if we do so, then repeated department and office information must be entered
each time the same teacher is assigned to teach another class. If one bit of information
changes (e.g., Mr. Brown moves from G105 to G110) then every occurrence of data
containing Mr. Brown's information must be located and updated to avoid data
inconsistency. A different problem arises if Mr. Brown takes a term off for any reason
and so does not teach a class. We would either have to retain a record with Mr.
Brown’s office and department information but with no values in the class name and
Access Database Design 4
number fields (a so-called insertion anomaly), or we would have to remove Mr. Brown
from the database altogether (a deletion anomaly).
Instead, we use two tables to represent the data, one with the minimal information
needed regarding the class and a second table with information about the teachers that
applies regardless of the classes they are teaching.
Class_Name Class_No Class_Teacher
Calculus 200 Mr. Brown
Interior Design 304 Ms Smith
Algebra 101 Mr. Brown
Geometry 110 Mr. Jones
Defining Relationships
For example: in a typical classroom environment Students, Teachers, and Classes
would be stored in separate tables.
• Students could be related to classes by transcript entries and grades.
• Teachers could be related to classes by teaching assignments.
• Classes could be related to students by enrollment and attendance.
To establish a relationship, we link one table's primary key to a foreign key in another
table. But, how do you decide which key to place in which table? To answer this
question, we must determine the nature of the relationship.
Types of Relationships
There are four basic types of relationships:
• One-to-many
• Many-to-one
• Many-to-many
• One-to-one
However, the one-to-many relationship is just the many-to-one relationship viewed in
the other direction, so we can consider them as one, leaving us with only three that we
need to examine.
One-to-Many Relationship (1 - ∞):
The one-to-many relationship is the most
common type. As an example, one teacher
might teach several classes. That is, the
teacher would only have one entry in the
Teacher table but may have several entries
in the Class table, depending upon teaching load. In this relationship, we always link the
primary key of the "one" side of the relationship (the source table) to the foreign key on
the "many" side (the target table). In our previous example, the primary key of the
teacher would be placed into each of the class records for classes that the teacher
taught. A search of the class table using the teacher key would produce a list of classes
taught by the teacher.
Many-to-Many Relationship (∞ - ∞):
In the many-to-many relationship, many
records from one table would be related to
many records in another table. For example:
many students would attend each class and
each student would attend many classes.
The icons under the heading “Featured Online Templates” are among the templates
upon which you can base your new database. You can click on
one of the icons in order to download the template from Microsoft
or select one of the categories listed to the left under “From
Microsoft Office Online” to see a somewhat broader selection of
templates in each of the categories.
When you select a template from any of these sources, a
description of it will appear on the right side of the Access
window, including a Download button that you can click to
retrieve the template after specifying your own name for the
database and choosing the location on your computer where it
will be stored. (The software will check that you are using a valid
copy of Access before the download proceeds.) An example
description appears to the right.
From the design work that you’ve done off-line, you should have a good idea of the
tables that you need to create, the fields to be stored in each table, and the
relationships between the tables. Now you’ll implement that design using the tools
provided by Access. For your first table, you can rename and modify Table1 using
Design View, or you can discard Table1 and use either of the other methods of creating
tables described below.
Creating Tables
Access provides at least four methods for creating tables.
We’ll look at the three that you can use without having a
SharePoint server available to you. All are initiated from the
Create ribbon, a portion of which is shown to the right.
Table Templates
The table templates menu on the Create ribbon offers a small
collection of pre-defined tables, shown to the right. If there is
a table in the list that comes close to meeting your needs,
you can select it to create the table and then modify the table
in Design View so that it matches your design precisely.
Table Design
If there is one table creation method with which you must be
familiar, it is Table Design, because it provides the means by
which you can adjust a table’s properties, however the table
was originally created. When you click on the Table Design
icon in the Create ribbon, you are presented with a
worksheet for specifying the fields in the new table. At a
minimum, you must supply the name of each field and select
the type of data it will hold. You can also provide a
description of the field and specify other properties such as
input mask and validation.
You can adjust the properties to meet the needs of your database. When you select
one of the properties, a brief description of it is displayed in the blue area to the right.
Further information is available by pressing the F1 key to access context-sensitive help.
In this example, you might change the Field Size of 255 characters, since that is much
larger than anyone’s middle name.
Some of a field’s properties can be used to reduce if not eliminate data entry errors by
imposing restrictions on what values are permitted in the field. An input mask,
validation rule, and other specifications can each help in some circumstances.
Input Mask
You can use an input mask to ensure that commonly used data like phone numbers,
dates, and zip codes are properly entered into the database. Here’s how:
1. Select the field for which you want to set an input mask by clicking on it in the list
of fields.
2. Click in the Input Mask line of the field’s properties. An icon containing an ellipsis
( ) will appear at the right end of the line.
3. Click on the ellipsis icon to open the Input Mask Wizard.
Requirred Data
Some fieeld values may
m be so critical that you wish too require th
hem in any record of your
y
table. For
F a table containing
c d
data about people, youu might insist on havin
ng a last na
ame
for each
h person in the databasse (unless, I suppose,, your datab
base includ
des Cher orr
T require a value for a field:
Sting). To
1. Select
S the fie
eld whose value
v is to be required
d by clicking
g on it in the
e list of field
ds.
2. C
Click in the Required
R lin
ne of the fie
eld’s properrties. An iccon indicatin
ng a drop-d
down
m
menu will ap
ppear at the
e right end of
o the line.
3. Click
C on the drop-down
n menu icon
n and selecct Yes from the list.
Note: Iff you speciffy a requireed value in a table thatt already haas data in
it, be sure to click on
o the “Test Validation n Rules” ico
on in the Deesign
ribbon too determine e if any exissting data already
a viola
ates the req
quirement.
Primarry Key
Access insists that each table
e have a primary key. If you try to
o save a neew table witthout
having specified
s a primary keyy, Access will
w offer to create a ke ey for you. There are,
thus, two
o ways to designate
d th
he primary key for a ta
able:
1. Iff there is no
o field in the
e table that can serve as a primarry key, you can accep pt the
offer that Ac ccess make es when you u save a table lacking a key. In that
t case, a
new field willl be inserte ed into the table.
t Typically, the name of the new field will
w
be ID and its s data type will be AutoNumber. As you entter data into o a table wiith
such a prima ary key, Acccess will suupply value
es in the ID field autom
matically,
beginning with
w 1 and in ncreasing sequentially.
2. Iff there is a field
f that co
ould be a ke
ey—such asa a WVUID D field in a table
t of
W
WVU emplo oyees or stu udents—the en you can manually specify
s it ass such.
S
Simply selecct the field in
i Design View
V and cliick on the Primary
P Keyy icon
in
n the Design ribbon.
Creatiing Rela
ationship
ps
The gen
neral proced
dure for cre
eating a rela
ationship in
n Microsoft Access
A is to
o:
• D
Determine th
he type of the
t relationsship and identify the source
s and target table
es.
• Iff necessaryy, create a linking or crross referen
nce table to
o serve as the
t common
taarget thereb
by resolve a many-to-m many relationship.
• C
Create the fo
oreign key field(s) in th
he target ta
able if they are not alre
eady presen
nt.
• S
Select the Database
D Tools ribbon
n.
• C
Click on the Relationships icon in
n the ribbon
n.
• A all of the tables invvolved in th
Add he relationsh
hip to the window.
w
• Create the relationship(s) by dragging the primary key(ss) from the source(s) and
C
dropping the
em on the associated
a f
foreign keyy(s) in the ta
arget(s).
Access Database
D gn 16
Desig
Relationship Pane
To open the relationship window go to Database Tools > Relationships.
If your database does not have any relationships defined, the Show Table dialog box
will automatically be displayed. Use it to add the tables you want to relate.
Once you have selected all the tables you want to relate, define a relationship between
two tables by dragging the field that you want to relate from one table to the related
field in the other table.
In most cases, you drag the primary key field (which is displayed with a key icon next
to it) from one table to a similar field (often with the same name) called the foreign key
in the other table. The related fields are not required to have the same names (though
it’s good practice to make them the same since it reminds you where the relationship
comes from), but they must have the same data type and contain the same kind of
information. In addition, when the matching fields are Number fields, they must have
the same Field Size property setting.
Once you have created the
relationships, the Edit Relationships
dialog box is displayed. Check the
field names displayed in the two
columns to ensure they are correct.
You can change them if necessary.
Access Da
atabase Design
n 19
Notes on Importing
• You can import or link all the data from a spreadsheet, or just the data from a
named range of cells.
• You can create a new table in Access based on the data.
• You can append the data to an existing table as long as your spreadsheet
column headings match the table's field names.
− If you attempt to append a table which contains a primary key field and your
data for that field contains null or duplicate values you will experience an
error.
• Access attempts to assign the appropriate data type to imported fields, but you
should check your fields to make sure that they are set to the data type you want.
− For example, in an Access database, a phone number or postal code field
might be imported as a Number field, but should be changed to a Text field
in Microsoft Access because it is unlikely that you will perform any
calculations on these types of fields. In the case of zip codes, leading
zeroes will be lost if the zip code data is brought into a Number field, but not
if it is brought into a Text field. You should also check and set field
properties, such as formatting, as necessary.
Notes on Linking
• If you link to a file and that file is deleted or moved from its original location (e.g.,
it is placed in a different folder or drive) the link to the file will be severed. You
then have to use the Linked Table Manager to re-establish the link if the file has
not been deleted.
• If you link to a file on a local area network, it is best to use a universal naming
convention (UNC) path, if you know it, rather than relying on the drive letter of a
mapped network drive in Microsoft Windows Explorer. A drive letter can vary on
a computer or may not always be defined, whereas a UNC path is a reliable and
consistent way for Microsoft Access to locate the data source that contains the
linked table.
• Universal naming convention for files that provides a machine-independent
means of specifying the file’s location. Rather than using a drive letter and path,
a UNC name uses the syntax \\server\share\path\filename.