MIS 70-451
Creating a Database
[This lab exercise was written by Evelyn Barry
If you have any questions or comments, contact her at eb48@andrew.cmu.edu]
Objective:
This exercise will step you through the process of creating a database in Access. For the purposes of this exercise we will concentrate on building three tables in a relational database for a library.
To Start:
1. From the Windows desktop, click on Applications.
2. Click on Databases.
3. Click on Access.
4. Click on "Create New Database using : blank database", then click on OK.
5. Access will immediately want to determine the name of the database and where it is to be located. Be sure to change the directory at the top of the Save small screen to the correct drive:, and then name the database "your name" at the bottom of the small screen. Then click on Create.
Create Tables:
Note: When you are done with this step, you should have created tables for 3 entities. I have attached several reference tables to help you fill in the blanks as you work through the steps below.
To create a new table:
Click on New.
Access will give you several options. Click on Design View, then OK.
For each table:
For each attribute (field) in the entity (table) enter the field name, data type and description columns.
Tab over to the data type column. The data type defaults to text. You can change this to another data type by clicking on the pull-down menu. See the attached table of data types to set the appropriate data type for each field. For embedded dashes like those in ISBN, you will want to use a text field. Data type Number should only be used for fields to be used in mathematical calculations.
Tab over to the description column and type a valid description. Tab again and it takes you to the next field name entry. Continue to enter field names, data types and descriptions until you have finished the table.
To create a primary key for a table:
Recall that each entity has to have a unique identifier, and that each entity relates to a table in the Access database. You now need to indicate to Access which of the fields you have created will serve as the primary key for the table. When choosing the fields to use as the primary key, remember that the primary key must be different for each instance or row in the table.
To create field properties:
Now we are ready to talk about specific field properties that will help to display data in a proper format, and which will help to prevent entry of bad data. This discussion will cover some of the basic field properties. For a more complete discussion of field properties, I suggest you read Access 7.0 by O'Leary and O'Leary, or Access for Dummies by Kaufeld.
Click on the first field in your table, and look at the field properties at the bottom on the screen.
Format:
Input Mask:
Default Value:
Validation Rule:
Validation Text:
Indexed:
To save a table:
Better safe than sorry. Save the table as often as possible. Click on the Save icon, or click on File and then on Save.
Entering data:
To enter data, click on View, then Data Sheet. You can now enter your data. Enter a few records to test the validation, input mask and formats to see if they are set the way you want. (I have attached sample tables of data for the Library database. You should try entering that data and then add some of your own if you would like.)
While entering data, purposely enter bad data in fields with validation rules and see what happens. Enter the ISBN without the hyphens, and see how the system fills them in for you. If these things are not working properly, go back to the design view of the table and make corrections. Then try again.
Save the table again after you have entered the data
Close the table and go back to create tables and enter data for the other two tables. (You will need all three tables to complete this exercise and be ready or tomorrow.)
Creating Relationships:
The last step is to create the relationships among the tables. A relationship can link a key field in one table to a field that contains the same values in another table. This is the mechanism Access uses to tie the data in the different tables of a database together. Note: A foreign key is a field which is a primary key in one table and a non-key attribute in another table. Foreign keys are used to establish relationships between tables in a database.Click on the relationship icon. The relationship screen will appear. (The example relationships screen is for the Northwind database.)
Double click on each of you tables in your database to bring them onto the relationship screen. If the list of tables does not appear, then click on Relationships, then on Show Table, to bring up the list. Once you've brought up all the tables, click on close on the Show Table screen.
To establish a relationship simply click on ISBN in one table. Holding the left mouse button down, drag ISBN over to ISBN in another table and drop it there. This will create a relationship between two tables using the ISBN key. Normally, relationships are established using foreign keys.
When you have established all the relationships for these three tables, close the relationship screen and save the relationship layout when prompted.
You should print copies of the contents of each of your tables.
Printing:
To print the contents of a table, list all the current tables in your database by clicking on the Tables object tab. Right click on the table you want to print. Then click on the Print command. It's just that easy!!
In later lab exercises we will learn about the other objects used to view data in a database; queries and forms.
Table 1: Data Types
Data Type |
Description |
Text |
any kind of text (letters, numbers, or both) |
Memo |
really long text entries (descriptions, reports, etc.) |
Number |
any number you intend to use for counting or calculations |
Autonumber |
Access automatically sets a unique number for each record in the database |
Currency |
numbers and symbols that represent quantities of money |
Date/Time |
dates, times or both |
Yes/No |
two-option logic values (yes/no, true/false, male/female) |
OLE |
Multimedia objects (Access 97 only) |
Hyperlink |
clickable links to WWW pages (Access 97 only) |
Table 2: Commonly Used Data Fields
Name |
Type |
Size |
Description |
Title |
Text |
4 |
Mr., Ms., Mrs., Sr. |
First Name |
Text |
15 |
|
Middle Initial |
Text |
4 |
Allows for two initials and punctuation |
Last Name |
Text |
20 |
|
Job |
Text |
25 |
Job title |
Company |
Text |
25 |
|
Address 1, Address 2 |
Text |
30 |
Include 2 fields for address because some corporate locations are pretty complicated |
City |
Text |
20 |
|
State, Province |
Text |
4 |
Apply the name appropriately for the data you’re storing |
Zip code, Postal code |
Text |
10 |
Use text, allow for hyphen in middle of 9 digit zip |
Country |
Text |
15 |
Only needed for international databases |
Telephone No. |
Text |
13 |
(999)999-9999; increase to 17 if storing extension no. |
E-mail address |
Text |
30 |
Full Internet e-mail address |
SSN |
Text |
11 |
9 digit SSN with hypehns |
Table 3: Codes for the Input Mask
Kind of Characters |
Code for an optional Field |
Code for a Required Field |
Digits 0 to 9 only |
0 |
9 |
Digits and +/- sign |
(not available) |
# |
Letters A to Z only |
L |
? |
Letters or digits only |
A |
a |
Any character or space |
& |
C |
Table 4: Commonly User Validations
Field Type |
Validation Rule |
Definition |
Number |
> 0 |
Must be a positive number (greater than 0) |
Number |
< > 0 |
Cannot be zero |
Number |
> 0 and < 100 |
Must be between 0 and 100 ( not including 0 or 100) |
Number |
> = 0 and < = 100 |
Must be between 0 and 100 (including 0 and 100) |
Date |
> = Date ( ) |
Must be today’s date or later |
Date |
> = Date ( ) or Is Null |
Must be today’s date, later or blank |
Date |
< Date ( ) |
Must be earlier than today’s date |
Date |
> = #1/1/90# And < = Date ( ) |
Must be between January 1, 1990, and today’s date (inclusive) |
Table 5: ISBN Master List
ISBN |
Title |
Author-Name |
Copyright-Date |
Publisher |
Bibliography- Indic |
Subject-category |
0-380-71754-4 |
The Plan |
Cannel, Stephen J. |
1995 |
Avon Books |
N |
Political intrigue |
0-380-71084-6 |
Caesar’ s Women |
McCullough, Colleen |
1997 |
Avon Books |
N |
Historical novel |
0-425-14758-4 |
Debt of Honor |
Clancy, Tom |
1994 |
Berkley Books |
N |
Political intrigue |
0-451-52623-6 |
Rob Roy |
Scott, Sir Walter |
1995 |
Signet Books |
N |
Classic |
0-440-22147-1 |
Runaway Jury |
Grisham, John |
1996 |
Doubleday |
N |
Contemporary fiction |
0-440-22060-2 |
The Chamber |
Grisham, John |
1994 |
Doubleday |
N |
Contemporary fiction |
0-440-21172-7 |
A Time to Kill |
Grisham, John |
1992 |
Doubleday |
N |
Contemporary fiction |
0-446-36550-5 |
Pleading Guilty |
Turow, Scott |
1993 |
Warner Books |
N |
Contemporary fiction |
Table 6: Inventory
ISBN |
Copies owned |
Copies in stacks |
Copies borrowed |
Copies being repaired |
Copies on order |
0-380-71754-4 |
12 |
5 |
4 |
3 |
2 |
0-380-71084-6 |
10 |
4 |
6 |
||
0-425-14758-4 |
15 |
5 |
5 |
5 |
|
0-451-52623-6 |
20 |
1 |
17 |
2 |
5 |
0-440-22147-1 |
12 |
2 |
8 |
2 |
4 |
0-440-22060-2 |
15 |
2 |
13 |
|
|
0-440-21172-7 |
18 |
3 |
15 |
6 |
|
0-446-36550-5 |
5 |
2 |
3 |
5 |
Build your own data for the Book Location table. Use Borrower ID 001, 002 and 003. Distribute the books at the Squirrel Hill, Monroeville and Oakland branch libraries. Allow a default of 21 days for Days-May-Keep. The books by John Grisham are so popular that they may only be kept 7 days. Make sure you enter at least 10 records in this table. (Theoretically this table should have one entry for each copy of each book listed in the Inventory file. That would make this exercise too long for our class time.)