Anda di halaman 1dari 7

Unit 2: Business Software Use & Technique — Section 3: Databases Page 1 of 7

— Part 5: Relational Databases

MS ACCESS 200
Next Year
5. RELATIONAL DATABASES Add
queries or
forms
Relational Databases based on
multiple
tables?
What is a Relational Database?
Flatfile vs. Relational Databases — A simple database with only a single table is called a flatfile
database. It may be good enough for a simple address database, or to hold recipes or a music
collection, but for serious database work you need a relational database.
A relational database is a database with multiple related tables.

Why does a powerful database need more than one table?


• to minimize data storage
• to save time in data entry
• to decrease errors
• to enable more powerful queries, forms and reports Note: This first section of the
The examples below will illustrate each of these points. handout is for reading and
learning only. You begin work
on your computer at Ex. 1.
Why do you need multiple tables?
Imagine that you are a teacher who wants to keep records of your students' marks, along with the
student’s personal information. You begin by creating fields for Name, Address, Home Phone, Subject,
and Mark as below.
Name Address Home Phone Subject Mark
Alan Adams 123 Avenue Rd. 416-123-1234 Functions & Relations 76
Belle Barker 234 Bristol Dr. 416-234-2345 Functions & Relations 83

After you add data for all the students in the Functions & Relations class, you want to add marks for other
courses, beginning with Alan's mark of 82 in Computer Engineering. Immediately you run into a problem
with this database structure, since each subject (Functions & Relations, Computer Engineering) is going to
need its own table, as below.
Name Address Home Phone Subject Mark
Alan Adams 123 Avenue Rd. 416-123-1234 Computer Engineering 82
Belle Barker 234 Bristol Dr. 416-234-2345 Computer Engineering 90

Second, in each table you have to repeat every student's name, address and phone number!
Entering all this repeated data (database experts call it redundant data) is inefficient, wasting time,
as well as increasing the potential for errors. The redundant data also uses up more storage space.

Now imagine what happens when just a single piece of personal information about a student changes,
such as Belle Barker's address. You face a long and tedious task: You have to find every table containing
information on Belle Barker, open each one, and change her address. Again, this is highly inefficient,
entailing a lot of redundant data, and greatly increases the potential for errors or omissions.

Jarvis Collegiate BTA301 2005-2006


Unit 2: Business Software Use & Technique — Section 3: Databases Page 2 of 7
— Part 5: Relational Databases

MS ACCESS 200
5. RELATIONAL DATABASES

Is there some other way of structuring the database, so that all the information about a student is
stored in a single record? Next you try a different structure with only one record for each student,
something like the following:
Name Address Home Phone Name of Mark Name of Mark for Name of etc.
Subject 1 for Subject 2 Subject 2 Subject 3
Subjec
Alan 123 416-123-1234 Functions t82
1 Computer 82 American etc.
Adams Avenue & Relations Engineering History
Rd.

But working with this complicated, rather messy database structure is nearly impossible. How many
subjects must you allow for? Allowing for too many creates unused fields, which wastes computer
resources; while allowing for too few will lead to future problems. Second when running queries on the
records of all your students, how will you know which column to look in to find "History 104"? How can you
average marks when they could be in various columns? Whenever you see a database that uses fields
that are extended or repeated in this manner, you know the data needs to be broken down into separate
tables.

So you now know why a flat file database can waste data storage, waste time required for data entry,
increase the potential for errors, and impede effective queries, forms and reports.

The relational database solution


Creating a relational database solves our teacher’s problem. First, you create 3 tables, one for students,
one for subjects, and one for marks. Then you tie the tables together (establish relationships among
them). In order to create a relationship between tables, they must have a field in common.

The teacher's three-table database


1. The Students table, of course, includes fields for the student's names. Rather than a single name field, it
is split into 2 fields, one for the surname and one for the first name, to make queries easier. Notice that, in
addition to name fields, another field is created to hold a unique code for each student, so that the
computer can distinguish between two students with the same name. Call this field StudentlD. Thus, the
Students table contains the following fields:
Students table
StudentlD Surname FirstName StreetAddress City PostalCode Home Phone

010203 Adams Alan 123 Avenue Rd. Toronto M1M1M1 416-123-1234

090807 Barker Belle 234 Bristol Dr. Toronto M2M 2M2 416-234-2345

Notes:
• StudentlD is a unique code for each student. Note: Do not name this simply ID or it may
conflict with fields in other tables
• Splitting the name into Surname and FirstName makes queries easier.
• Splitting the address into 3 parts makes queries easier.

Jarvis Collegiate BTA301 2005-2006


Unit 2: Business Software Use & Technique — Section 3: Databases Page 3 of 7
— Part 5: Relational Databases

MS ACCESS 200
5. RELATIONAL DATABASES

2. The Subjects table will have fields for SubjectID, Subject, and Notes, as follows:
Subjects table
SubjectID Subject Notes
MCR301 Functions & Relations University Preparation
CHA301 American History University Preparation

Notes:
• SubjectID is a unique code for each subject, (e.g. the school's subject code).
Notice that name this simply ID could conflict with a field in the previous table.
• Subject is the full title of the subject.
• Notes is a field for comments or a brief description of what the subject
covers.

Since there is no common field to the Student, and Subjects tables, no direct relationship can
be established between them. A third table ties everything together.

3. The Marks table can pull all of the data together with only 3 fields, StudentID, SubjectID, and Mark:
Marks table
StudentID SubjectID Mark
010203 MCR301 76
010203 CHA30| 82
090807 MCR301 61
090807 CHA30| 90

Notes:
• StudentID is also part of the Students table, so a relationship can be
established with the Students table.
• SubjectID is also part of the Subjects table, so a relationship can be
established with the Subjects table.
• Mark is the mark this student achieved in this subject.

Now that this table is related to the 2 other tables by means of the StudentID and SubjectID fields, a full
report can be created displaying the all student's marks together with any or all persona! information
about the student and information about the subject. And, of course, queries and forms can be created for
multiple tables.

In summary, the advantages of using a relational database for the teacher's data are:
• minimized redundant data
• minimized data entry
• time savings
• reduced possibility of errors
• more powerful queries, forms and reports

Note: As you probably remember, when you create a table, Access asks if you want to create a primary
key. In BTT we declined to include a primary key, but it is an important component of a sophisticated,
relational database. The primary key is a field which gives each record a unique identifier—usually a
number, which Access adds automatically. This is used to establish relationships with other tables.
Although it is possible to use another field as a basis for relationships, problems will arise if records exist

Jarvis Collegiate BTA301 2005-2006


Unit 2: Business Software Use & Technique — Section 3: Databases Page 4 of 7
— Part 5: Relational Databases

MS ACCESS 200
5. RELATIONAL DATABASES

with duplicated data (e.g. 2 customers surnamed Jones).

Ex. 1. Creating a simple relational database


We'll take the easy way to produce this simple relational database, first creating the 3 tables in Excel,
then importing them into Access.
1. Open Excel. Follow the examples below to create and save 3 files, Students, Subjects, Marks.
Close Excel.

StudentlD Surname FirstName StreetAddress City PostalCode Home Phone

010203 Adams Alan 123 Avenue Rd. Toronto M1M1M1 416-123-1234

090807 Barker Belle 234 Bristol Dr. Toronto M2M 2M2 416-234-2345

SubjectID Subject Notes


MCR301 Functions & Relations University Preparation
CHA301 American History University Preparation

StudentID SubjectID Mark


010203 MCR301 76
010203 CHA30| 82
090807 MCR301 61
090807 CHA30| 90

2. Open Access, creating a new database named TeachersMarks. Import each of the 3 tables
into TeachersMarks. In the Students table, set StudentID as the primary key. Do NOT add a
primary key to either the Subjects or Marks table.
3. Edit each of the 3 tables in Design View to set Data Type and Field Size as indicated.
(Reducing field sizes will keep from wasting space and make reports easier to fit on a page.)
Close all the tables.

Students table
Field Data Type Field Size
StudentID number
Surname text 15
FirstName text 15
PostalCode text 7
HomePhone text 12
All other fields text 20

Subjects table
Field Data Type Field Size
SubjectID text 6
Subject text 30
Notes text 255

Marks table
Field Data Type Field Size
Mark number
SubjectID text 6 '

Jarvis Collegiate BTA301 2005-2006


Unit 2: Business Software Use & Technique — Section 3: Databases Page 5 of 7
— Part 5: Relational Databases

MS ACCESS 200
5. RELATIONAL DATABASES

StudentID number

Creating relationships between the tables


4. Select Tools / Relationships. The Relationships window opens displaying the Show Table
dialogue box; the Tables tab is selected. Select each of the 3 tables one by one and click on
Add for each. Close the Show Table dialogue box. The Relationships window now shows the
3 tables waiting for you to establish connections between common fields.
5. In the Marks table click and hold StudentID and drag it—notice that the mouse pointer
becomes a rectangle— PRECISELY onto StudentID in the Students table. Release the mouse button.
An Edit Relationships dialogue button opens. Accept the defaults and click on Create. A
connecting line is now drawn between the two related fields.
6. Establish a similar relationship between the SubjectID field in the Marks table and the
SubjectID field in the Subjects table. Drag the move the Subjects table in the Relationships
window so that all the connecting lines can be seen clearly.
7. Save and Close the relationships window.

Ex. 2. Creating a report from multiple tables


1. In the Objects pane of the database's main window, click on Reports. Double-click on
Create report by using wizard.
2. In the Report Wizard window, under Tables/Queries select the Students table. Use the
arrow (>) buttons to move the fields Surname, then FirstName into the Selected Fields
box.
3. From the Subjects field, move the fields SubjectID and Subject into the Selected Fields
box.
4. From the Marks field, move the field Marks into the Selected Fields box.
5. Click on Next 3 times to accept the defaults. Choose Landscape orientation, and click
on Next. Click on Next again. Name the report Students Marks, and click on Finish.
The completed report will appear. Print the report.

Examining a sample “real world” relational database


Open the relationships window of Northwind Traders sample database to see its eight tables and seven
relationships (below). Examine it to see if you can figure out the purpose of each table, why data is split
into separate tables and, overall, how their customer order system works.

Jarvis Collegiate BTA301 2005-2006


Unit 2: Business Software Use & Technique — Section 3: Databases Page 6 of 7
— Part 5: Relational Databases

MS ACCESS 200
5. RELATIONAL DATABASES

Notice the symbol 1 and the infinity symbol (∞) at the ends of the line joining the Customers table to the
Orders table. They indicate the type of relationship between the tables. Each customer (i.e. each record in
the Customer table) can have many orders (i.e. many records in the Orders table). This is called a one-
to-many relationship. In a one-to-many relationship, a record in Table A can relate to zero, one, or many
records in Table B. Many records in Table B can relate to one record in Table A. It is the most common
type of relationship.
The second type of relationship is the one-to-one relationship where each record in Table
A relates to one, and only one, record in Table B, and each record in Table B relates to one,
and only one, record in Table A. In the example above, a one-to-one relationship can be seen
between the Orders table and the Shipping Methods table, since each order can have only one
shipping method. Below is another example of tables with a one-to-one relationship, showing
tables from a company's Employees database: Each record in the Personal table is about one
employee. That record relates to one, and only one, record in the Payroll table. Each record in
the Payroll table relates to one, and only one, record in the Personal table.

Illustration: Tables with a one-to-one relationship from a database of information about employees

The one-to-one relationship type is not used often, because most information related in this
way would be in one table.
In the example above it is used to isolate part of a table for security reasons (to keep the
employee's pay rate confidential). It might be used to divide a table with many fields, or to store
information that applies only to a subset of the main table. For example, you might want to
create a table to track employees participating in a fundraising hockey game.

Deciding on the type of


relationship to establish between
two tables can become difficult in
a large and complex database.
Luckily, Access makes its own
guesses—usually one-to-many—
and it is usually correct.
Here you are getting a very brief
introduction just to the concepts of
database relationships. The topic
will be more of a concern to you if
databases become part of your
profession.

Jarvis Collegiate BTA301 2005-2006


Unit 2: Business Software Use & Technique — Section 3: Databases Page 7 of 7
— Part 5: Relational Databases

MS ACCESS 200
5. RELATIONAL DATABASES

Ex. 3. Creating a relational database for a bank


1. Create a relational database for a bank with 3 tables with the field names and data shown below. Use
appropriate data types, field sizes, etc. (For this exercise, do not use input masks.)

Customer table
CustomerlD Name Address City Prov PCode
00512 Mr. Halm 112 Islington Ave. Markham ON M1M 1A1
03471 Mrs. Brown 223 Davis Ave. Toronto __ON__ M2M 2B2
00292 Mr. Cowley 334 Miller Dr Etobicoke ON M3M 3C3
01485 Mr. & Mrs. Chu 445 Maple Rd. Streetsville ON M4M 4D4

Accounts table
CustomerlD AccountNo. AccountType DateOpened Balance
00512 8858 Chequing 10/10/1989 $4000.00
00512 8859 Savings 10/10/1989 $2000.00
03471 5671 Savings 01/02/1996 $1000.00
00292 4422 Chequing 12/01/1999 $6000.00
00292 4433 Savings 12/01/1999 $9000.00
01485 6543 Savings 09/09/1994 $500.00
01485 6544 Chequing 11/11/1988 $800.00

Services table
CustomerlD CreditCard MortgageNo.
00512 Yes 43211234
03471 Yes 45677654
01485 Yes 98766789
01485 Yes 65433456

2. Establish relevant relationships between the tables using common fields. Print the Relationships in report
form (File / Print Relationships). Save the report as Relationships.

Trouble-Shooting a Relational Database


When a report or query based on a relational database doesn’t work correctly, the most common
sources of error are:
• relationships established between fields which are not common
• common fields assigned different data types in different tables
• bad records in tables (often resulting from poor importing of data)
• data in common fields entered incorrectly

Jarvis Collegiate BTA301 2005-2006

Anda mungkin juga menyukai