Relational Databases: MSA 2003 5. R D
Relational Databases: MSA 2003 5. R D
MS ACCESS 2003
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.
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.
MS ACCESS 2003
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.
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.
MS ACCESS 2003
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
MS ACCESS 2003
5. RELATIONAL DATABASES
090807 Barker Belle 234 Bristol Dr. Toronto M2M 2M2 416-234-2345
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 '
MS ACCESS 2003
5. RELATIONAL DATABASES
StudentID number
MS ACCESS 2003
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.
MS ACCESS 2003
5. RELATIONAL DATABASES
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.