You are on page 1of 62

Relational Databases

Chapter 4

Copyright © 2015 Pearson Education, Inc.


4-1
Learning Objectives
• Explain the importance and advantages of databases, as well as the
difference between database and file-based legacy systems.

• Explain the difference between logical and physical views of a


database.

• Explain fundamental concepts of database systems such as DBMS,


schemas, the data dictionary, and DBMS languages.

• Describe what a relational database is and how it organizes data.

• Create a set of well-structured tables to properly store data in a


relational database.

• Perform simple queries using the Microsoft Access database.

Copyright © 2015 Pearson Education, Inc.


4-2
What Is a Database?
• Efficiently and centrally coordinates information for a
related group of files
• A file is a related group of records
• A record is a related group of fields
• A field is a specific attribute of
interest for the entity (record)

Copyright © 2015 Pearson Education, Inc.


4-3
FILE VS. DATABASES
• Let’s examine some basic principles about how data are
stored in computer systems.
▫ An entity is anything about which the organization wishes to
store data. At your college or university, one entity would be the
student.

STUDENT
Student
Student Student Phone Student
Student ID Last Name First Name Number Birth Date

333-33-3333 Simpson Alice 333-3333 10/11/84

111-11-1111 Sanders Ned 444-4444 11/24/86

123-45-6789 Moore Artie 555-5555 04/20/85


Copyright © 2015 Pearson Education, Inc.
FILE VS. DATABASES
▫ Information about the attributes of an entity (e.g.,
the student’s ID number and birth date) are stored in
fields.
▫ A field is the physical space where an attribute is
stored.
STUDENT
Student
Student Student Phone Student
Student ID Last Name First Name Number Birth Date

333-33-3333 Simpson Alice 333-3333 10/11/84

111-11-1111 Sanders Ned 444-4444 11/24/86

123-45-6789 Moore Artie 555-5555 04/20/85

Copyright © 2015 Pearson Education, Inc.


FILE VS. DATABASES
▫ All the fields containing data about one entity (e.g.,
one student) form a record.
▫ A record is the set of attributes stored for a particular
instance of an entity.
▫ The example below shows the record for Artie Moore.

STUDENT
Student
Student Student Phone Student
Student ID Last Name First Name Number Birth Date

333-33-3333 Simpson Alice 333-3333 10/11/84

111-11-1111 Sanders Ned 444-4444 11/24/86

123-45-6789 Moore Artie 555-5555 04/20/85


Copyright © 2015 Pearson Education, Inc.
FILE VS. DATABASES
• A data value is the intersection of the row and column.
• The data value for Ned Sanders’ birth date is 11/24/86.

STUDENT
Student
Student Last Student Phone Student
Student ID Name First Name Number Birth Date
333-33-3333 Simpson Alice 333-3333 10/11/84
111-11-1111 Sanders Ned 444-4444 11/24/86

123-45-6789 Moore Artie 555-5555 04/20/85

Copyright © 2015 Pearson Education, Inc.


FILE VS. DATABASES
▫ A set of all related records forms a file (e.g., the
student file).
▫ If this university only had three students and five fields
for each student, then the entire file would be depicted
below.
STUDENT
Student
Student Student Phone Student
Student ID Last Name First Name Number Birth Date

333-33-3333 Simpson Alice 333-3333 10/11/84

111-11-1111 Sanders Ned 444-4444 11/24/86

123-45-6789 Moore Artie 555-5555 04/20/85


Copyright © 2015 Pearson Education, Inc.
FILE VS. DATABASES
▫ A set of interrelated, centrally coordinated files forms
a database.
▫ When files about students are integrated with files
about classes and files about advisors, we have a
database.

Student
File Class
File
Advisor
File
Copyright © 2015 Pearson Education, Inc.
FILE VS. DATABASES
• Database systems were developed to address the
problems associated with the proliferation of
master files.
▫ For years, each time a new information need
arose, companies created new files and programs.
▫ The result: a significant increase in the number of
master files.

Copyright © 2015 Pearson Education, Inc.


Databases and Files

Copyright © 2015 Pearson Education, Inc.


Advantages of Databases
• Data is integrated and easy to share
• Minimize data redundancy
• Data is independent of the programs that use the
data
• Data is easily accessed for reporting and cross-
functional analysis

Copyright © 2015 Pearson Education, Inc.


4-4
Database Users and Designers
• Different users of the database information are
at an external level of the database. These users
have logical views of the data.
• At an internal level of the database is the
physical view of the data which is how the data
is actually physically stored in the system.
• Designers of a database need to understand
user’s needs and the conceptual level of the
entire database as well as the physical view.

Copyright © 2015 Pearson Education, Inc.


4-5
Copyright © 2015 Pearson Education, Inc.
Copyright © 2015 Pearson Education, Inc.
DATABASE SYSTEMS
Schemas
- A schema describes the logical structure of a
database.
- There are three levels of schema.
– Conceptual level
– External level
– Internal level

Copyright © 2015 Pearson Education, Inc.


SCHEMAS

Copyright © 2015 Pearson Education, Inc.


Database Design
• To design a database, you need to have a
conceptual view of the entire database. The
conceptual view illustrates the different files and
relationships between the files.

• The data dictionary is a “blueprint” of the


structure of the database and includes data
elements, field types, programs that use the data
element, outputs, and so on.

Copyright © 2015 Pearson Education, Inc.


4-6
DATABASE SYSTEMS
• Information provided for each element includes:
▫ A description or explanation of the element.
▫ The records in which it is contained.
▫ Its source.
▫ The length and type of the field in which it is stored.
▫ The programs in which it is used.
▫ The outputs in which it is contained.
▫ The authorized users of the element.
▫ Other names for the element.

Copyright © 2015 Pearson Education, Inc.


DATABASE SYSTEMS

• DBMS Languages
▫ Every DBMS must provide a means of
performing the three basic functions of:
– Creating a database
– Changing a database
– Querying a database

Copyright © 2015 Pearson Education, Inc.


DBMS Languages
• Data Definition Language (DDL)
▫ Builds the data dictionary
▫ Creates the database
▫ Describes logical views for each user
▫ Specifies record or field security constraints
• Data Manipulation Language (DML)
▫ Changes the content in the database
– Creates, updates, insertions, and deletions
• Data Query Language (DQL)
▫ Enables users to retrieve, sort, and display specific
data from the database

Copyright © 2015 Pearson Education, Inc.


4-7
Relational Database

• Represents the conceptual and external schema


as if that “data view” were truly stored in one
table.
• Although the conceptual view appears to the
user that this information is in one big table, it
really is a set of tables that relate to one another.

Copyright © 2015 Pearson Education, Inc.


4-8
RELATIONAL DATABASES
• A DBMS is characterized by the type of logical
data model on which it is based.
▫ A data model is an abstract representation of the
contents of a database.
▫ Most new DBMSs are called relational
databases because they use the relational model
developed by E.F. Codd in 1970.
• The relational data model represents everything
in the database as being stored in the forms of
tables (aka, relations).

Copyright © 2015 Pearson Education, Inc.


A Relational Table
Row (Record)

Each row contains multiple attributes Same type of data


describing an instance of the entity. In this
case, inventory. Column (Field)

Copyright © 2015 Pearson Education, Inc.


STUDENT
Last First Phone
Student ID Name Name No.
333-33-3333 Simpson Alice 333-3333
111-11-1111 Sanders Ned 444-4444
123-45-6789 Moore Artie 555-5555

Course ID Course Name Section Day Time


1234 ACCT-3603 1 MWF 8:30
1235 ACCT-3603 2 TR 9:30
1236 MGMT-2103 1 MW 10:00

STUDENT-COURSE
Student ID Course ID
A primary key is the
333-33-3333 1234 attribute or combination
333-33-3333 1236 of attributes that
111-11-1111 1235 uniquely identifies a
111-11-1111 1236 specific row in a table.
Copyright © 2015 Pearson Education, Inc.
STUDENT
First Advisor
Student ID Last Name Name Phone No. No.
333-33-3333 Simpson Alice 333-3333 1418
111-11-1111 Sanders Ned 444-4444 1418
123-45-6789 Moore Artie 555-5555 1503

ADVISOR
Advisor No. Last Name First Name Office No.
1418 Howard Glen 420
1419 Melton Amy 316
1503 Zhang Xi 202
1506 Radowski J.D. 203

A foreign key is an attribute in one table that is a primary key in


another table. Used to link the two tables.
Copyright © 2015 Pearson Education, Inc.
Database Design Errors
• If database is not designed properly data errors
can occur.
▫ Update Anomaly
– Changes to existing data are not correctly recorded.
– Due to multiple records with the same data attributes
▫ Insert Anomaly
– Unable to add a record to the database.
▫ Delete Anomaly
– Removing a record also removes unintended data from the
database.

Copyright © 2015 Pearson Education, Inc.


RELATIONAL DATABASES
• Alternatives for Storing Data
▫ One possible alternate approach would be to store
all data in one uniform table.
▫ For example, instead of separate tables for
students and classes, we could store all data in one
table and have a separate line for each student -
class combination.

Copyright © 2015 Pearson Education, Inc.


Last First
Student ID Name Name Phone No. Course No. Section Day Time
333-33-3333 Simpson Alice 333-3333 ACCT-3603 1 M 9:00 AM
333-33-3333 Simpson Alice 333-3333 FIN-3213 3 Th 11:00 AM
333-33-3333 Simpson Alice 333-3333 MGMT-3021 11 TH 12:00 PM
111-11-1111 Sanders Ned 444-4444 ACCT-3433 2 T 10:00 AM
111-11-1111 Sanders Ned 444-4444 MGMT-3021 5 W 8:00 AM
111-11-1111 Sanders Ned 444-4444 ANSI-1422 7 F 9:00 AM
123-45-6789 Moore Artie 555-5555 ACCT-3433 2 T 10:00 AM
123-45-6789 Moore Artie 555-5555 FIN-3213 3 Th 11:00 AM

• Using the suggested approach, a student taking three classes


would need three rows in the table.
• In the above, simplified example, a number of problems arise.

Copyright © 2015 Pearson Education, Inc.

© 2012 Pearson Education, Inc. Accounting Information Systems, Romney/Steinbart


Last First
Student ID Name Name Phone No. Course No. Sect. Day Time
333-33-3333 Simpson Alice 333-3333 ACCT-3603 1 M 9:00 AM
333-33-3333 Simpson Alice 333-3333 FIN-3213 3 Th 11:00 AM
333-33-3333 Simpson Alice 333-3333 MGMT-3021 11 TH 12:00 PM
111-11-1111 Sanders Ned 444-4444 ACCT-3433 2 T 10:00 AM
111-11-1111 Sanders Ned 444-4444 MGMT-3021 5 W 8:00 AM
111-11-1111 Sanders Ned 444-4444 ANSI-1422 7 F 9:00 AM
123-45-6789 Moore Artie 555-5555 ACCT-3433 2 T 10:00 AM
123-45-6789 Moore Artie 555-5555 FIN-3213 3 Th 11:00 AM

• Suppose Alice Simpson changes her phone number. You need to


make the change in three places. If you fail to change it in all three
places or change it incorrectly in one place, then the records for
Alice will be inconsistent.
• This problem is referred to as an update anomaly.
Copyright © 2015 Pearson Education, Inc.

© 2012 Pearson Education, Inc. Accounting Information Systems, Romney/Steinbart


Last First
Student ID Name Name Phone No. Course No. Sect. Day Time
333-33-3333 Simpson Alice 333-3333 ACCT-3603 1 M 9:00 AM
333-33-3333 Simpson Alice 333-3333 FIN-3213 3 Th 11:00 AM
333-33-3333 Simpson Alice 333-3333 MGMT-3021 11 TH 12:00 PM
111-11-1111 Sanders Ned 444-4444 ACCT-3433 2 T 10:00 AM
111-11-1111 Sanders Ned 444-4444 MGMT-3021 5 W 8:00 AM
111-11-1111 Sanders Ned 444-4444 ANSI-1422 7 F 9:00 AM
123-45-6789 Moore Artie 555-5555 ACCT-3433 2 T 10:00 AM
123-45-6789 Moore Artie 555-5555 FIN-3213 3 Th 11:00 AM

• What happens if you have a new student to add, but he hasn’t


signed up for any courses yet?
• Or what if there is a new class to add, but there are no students
enrolled in it yet? In either case, the record will be partially blank.
• This problem is referred to as an insert anomaly.
Copyright © 2015 Pearson Education, Inc.

© 2012 Pearson Education, Inc. Accounting Information Systems, Romney/Steinbart


Last First
Student ID Name Name Phone No. Course No. Sect. Day Time
333-33-3333 Simpson Alice 333-3333 ACCT-3603 1 M 9:00 AM
333-33-3333 Simpson Alice 333-3333 FIN-3213 3 Th 11:00 AM
333-33-3333 Simpson Alice 333-3333 MGMT-3021 11 TH 12:00 PM
111-11-1111 Sanders Ned 444-4444 ACCT-3433 2 T 10:00 AM
111-11-1111 Sanders Ned 444-4444 MGMT-3021 5 W 8:00 AM
111-11-1111 Sanders Ned 444-4444 ANSI-1422 7 F 9:00 AM
123-45-6789 Moore Artie 555-5555 ACCT-3433 2 T 10:00 AM
123-45-6789 Moore Artie 555-5555 FIN-3213 3 Th 11:00 AM

• If Ned withdraws from all his classes and you eliminate all three of
his rows from the table, then you will no longer have a record of
Ned. If Ned is planning to take classes next semester, then you
probably didn’t really want to delete all records of him.
• This problem is referred to as a delete anomaly.
Copyright © 2015 Pearson Education, Inc.

© 2012 Pearson Education, Inc. Accounting Information Systems, Romney/Steinbart


RELATIONAL DATABASES
• Alternatives for Storing Data
▫ Another possible approach would be to store each
student in one row of the table and create multiple
columns to accommodate each class that he is
taking.

Copyright © 2015 Pearson Education, Inc.


Last First Phone
Student ID Name Name No. Class 1 Class 2 Class 3 Class 4

333-33-3333 Simpson Alice 333-3333 ACCT-3603 FIN-3213 MGMT-3021

111-11-1111 Sanders Ned 444-4444 ACCT-3433 MGMT-3021 ANSI-1422

123-45-6789 Moore Artie 555-5555 ACCT-3433 FIN-3213

• This approach is also fraught with problems:


– How many classes should you allow for in building the table?
– The above table is quite simplified. In reality, you might need to
allow for 20 or more classes (assuming a student could take
many 1-hour classes). Also, more information than just the
course number would be stored for each class. There would be
a great deal of wasted space for all the students taking fewer
than the maximum possible number of classes.
– Also, if you wanted a list of every student taking MGMT-3021,
notice that you would have to search multiple attributes.

Copyright © 2015 Pearson Education, Inc.

© 2012 Pearson Education, Inc. Accounting Information Systems, Romney/Steinbart


STUDENT
Last First Phone
StudentID Name Name No.
333-33-3333 Simpson Alice 333-3333
111-11-1111 Sanders Ned 444-4444
123-45-6789 Moore Artie 555-5555

COURSE
CourseID Course Section Day Time
1234 ACCT-3603 1 MWF 8:30
1235 ACCT-3603 2 TR 9:30
1236 MGMT-2103 1 MW 8:30
STUDENT COURSE • The solution to the preceding problems
StudentID CourseID is to use a set of tables in a relational
333-33-3333 1234 database.
333-33-3333 1236 • Each entity is stored in a separate table,
111-11-1111 1235 and separate tables or foreign keys can
111-11-1111
Copyright 1236Inc.
© 2015 Pearson Education, be used to link the entities together.
© 2012 Pearson Education, Inc. Accounting Information Systems, Romney/Steinbart
• Add a
STUDENTS
student
Last First Phone
here.
Student ID Name Name No.
333-33-3333 Simpson Alice 333-3333 • Leaves no
111-11-1111 Sanders Ned 444-4444 blank
123-45-6789 Moore Artie 555-5555
spaces.

COURSES
Course ID Course Section Day Time
1234 ACCT-3603 1 MWF 8:30
1235 ACCT-3603 2 TR 9:30
1236 MGMT-2103 1 MW 8:30
STUDENT COURSE
StudentID CourseID • Add a course here.
333-33-3333 1234 • Leaves no blank spaces.
333-33-3333 1236
111-11-1111 1235 • When a particular student enrolls for a
111-11-1111 1236 Inc.
Copyright © 2015 Pearson Education,
particular course, add that info here.
© 2012 Pearson Education, Inc. Accounting Information Systems, Romney/Steinbart
STUDENTS
Last First Phone • Ned still
Student ID Name Name No. exists in
333-33-3333 Simpson Alice 333-3333 the
111-11-1111 Sanders Ned 444-4444 student
123-45-6789 Moore Artie 555-5555 table.

COURSES
Course ID Course Section Day Time
1234 ACCT-3603 1 MWF 8:30
1235 ACCT-3603 2 TR 9:30
1236 MGMT-2103 1 MW 8:30
STUDENT COURSE
StudentID CourseID • Even if Ned was the only student in
the class, ACCT-3603 still exists in
333-33-3333 1234
the course table.
333-33-3333 1236
111-11-1111 1235 • If Ned Sanders drops ACCT-3603,
111-11-1111 1236 Inc.
Copyright © 2015 Pearson Education, remove Ned’s class from this table.
© 2012 Pearson Education, Inc. Accounting Information Systems, Romney/Steinbart
RELATIONAL DATABASES
• Basic Requirements of a Relational
Database
▫ Every column in a row must be single valued.
– In other words, every cell can have one and only one
value.
– In the student table, you couldn’t have an attribute
named “Phone Number” if a student could have
multiple phone numbers.
– There might be an attribute named “local phone
number” and an attribute named “permanent phone
number.”
– You could not have an attribute named “Class” in the
student table, because a student could take multiple
Copyright © 2015 classes.
Pearson Education, Inc.
RELATIONAL DATABASES
• Basic Requirements of a Relational
Database
▫ The primary key cannot be null.
– The primary key uniquely identifies a specific row in
the table, so it cannot be null, and it must be unique
for every record.
– This rule is referred to as the entity integrity
rule.

Copyright © 2015 Pearson Education, Inc.


RELATIONAL DATABASES
• Basic Requirements of a Relational
Database
▫ A foreign key must either be null or correspond to
the value of a primary key in another table.
– This rule is referred to as the referential
integrity rule.
– The rule is necessary because foreign keys are used
to link rows in one table to rows in another table.

Copyright © 2015 Pearson Education, Inc.


STUDENT
First Advisor
Student ID Last Name Name Phone No. No.
333-33-3333 Simpson Alice 333-3333 1418
111-11-1111 Sanders Ned 444-4444 1418
123-45-6789 Moore Artie 555-5555 1503

ADVISOR
Advisor No. Last Name First Name Office No.
1418 Howard Glen 420
1419 Melton Amy 316
1503 Zhang Xi 202
1506 Radowski J.D. 203

Advisor No. is a foreign key in the STUDENTS table. Every


incident of Advisor No. in the STUDENTS table either matches
an instance of the primary key in the ADVISORS table or is null.
Copyright © 2015 Pearson Education, Inc.

© 2012 Pearson Education, Inc. Accounting Information Systems, Romney/Steinbart


RELATIONAL DATABASES
• Basic Requirements of a Relational
Database
▫ All non-key attributes in a table should describe a
characteristic of the object identified by the
primary key.
– Could nationality be a non-key attribute in the
student table?
– Could advisor’s nationality be a non-key
attribute in the student table?

Copyright © 2015 Pearson Education, Inc.


RELATIONAL DATABASES
• There are two basic ways to design well-
structured relational databases.
▫ Normalization
▫ Semantic data modeling

Copyright © 2015 Pearson Education, Inc.


RELATIONAL DATABASES
• Normalization
▫ Starts with the assumption that everything is
initially stored in one large table.
▫ A set of rules is followed to decompose that initial
table into a set of normalized tables.
▫ Objective is to produce a set of tables in third-
normal form (3NF) because such tables are free of
update, insert, and delete anomalies.
▫ Approach is beyond the scope of this book but can
be found in any database textbook.

Copyright © 2015 Pearson Education, Inc.


RELATIONAL DATABASES
• Semantic data modeling (covered in detail in
Chapters 17 and 18)
▫ Database designer uses knowledge about how
business processes typically work and the
information needs associated with transaction
processing to draw a graphical picture (ERD) of
what should be included in the database.
▫ The resulting graphic is used to create a set of
relational tables that are in 3NF.

Copyright © 2015 Pearson Education, Inc.


RELATIONAL DATABASES
• Advantages over simply following normalization
rules:
▫ Semantic data modeling uses the designer’s
knowledge about business processes and
practices; it therefore facilitates efficient design of
transaction processing databases.
▫ The resulting graphical model explicitly represents
information about the organization’s business
processes and policies and facilitates
communication with intended users.

Copyright © 2015 Pearson Education, Inc.


IMPORTANCE AND ADVANTAGES OF DATABASE
SYSTEMS
• As accountants, you are likely to audit or work
for companies that use database technology to
store, process, and report accounting
transactions.
▫ Many accountants work directly with databases and
will enter, process, and query databases.
▫ Some will develop and evaluate internal controls
necessary to ensure database integrity.
▫ Others will be involved in the design and management
of databases.

Copyright © 2015 Pearson Education, Inc.


Copyright © 2015 Pearson Education, Inc.
TYPES OF ATTRIBUTES

Copyright © 2015 Pearson Education, Inc.


Copyright © 2015 Pearson Education, Inc.
Copyright © 2015 Pearson Education, Inc.
Copyright © 2015 Pearson Education, Inc.
Conceptual View Example

Customer Name Sales Invoice # Invoice Total


D. Ainge 101 $1,447
G. Kite 102 $4,394
D. Ainge 103 $ 898
G. Kite 104 $ 789
F. Roberts 105 $3,994

Copyright © 2015 Pearson Education, Inc.


4-9
Relational Data Tables

Copyright © 2015 Pearson Education, Inc.


4-10
Relational Data Tables

Primary Keys

Foreign Key (Customer # is a Foreign


key in the Sales Table because it is a
Primary key that uniquely identifies
Customers in the Customer Table).
Because of this, the Sales Table can relate
to the Customer Table (see red arrow
above).
Copyright © 2015 Pearson Education, Inc.
4-11
Why Have a Set of Related Tables?
• Data stored in one large table can be redundant
and inefficient causing the following problems:
▫ Update anomaly
▫ Insert anomaly
▫ Delete anomaly

Copyright © 2015 Pearson Education, Inc.


4-12
Relational Database Design Rules

• Every column in a row must be single valued


• Primary key cannot be null (empty) also known as entity integrity
• IF a foreign key is not null, it must have a value that corresponds to
the value of a primary key in another table (referential integrity)
• All other attributes in the table must describe characteristics of the
object identified by the primary key

Following these rules allows databases to be normalized and solves the


update, insert, and delete anomalies.
Copyright © 2015 Pearson Education, Inc.
4-13
Queries

• Users may want specific information found in a


relational database and not have to sort through
all the files to get that information. So they query
(ask a question) the data.
• An example of a query might be: What are the
invoices of customer D. Ainge and who was the
salesperson for those invoices?

Copyright © 2015 Pearson Education, Inc.


4-14
4-16

Creating the Query

Copyright © 2015 Pearson Education, Inc.


4-15
Query Answer

Copyright © 2015 Pearson Education, Inc.


4-16
Key Terms
• Database • External-level schema
• Database management system • Subschema
(DBMS) • Internal-level schema
• Database system • Data dictionary
• Database administrator (DBA) • Data definition language
• Data warehouse (DDL)
• Business intelligence • Data manipulation language
• Online analytical processing (DML)
(OLAP) • Data query language (DQL)
• Data mining • Report writer
• Record layout • Data model
• Logical view • Relational data model
• Physical view • Tuple
• Schema • Primary key
• Conceptual-level schema • Foreign key
Copyright © 2015 Pearson Education, Inc.
4-17
Key Terms (continued)
• Update anomaly
• Insert anomaly
• Delete anomaly
• Relational database
• Entity integrity rule
• Referential integrity rule
• Normalization
• Semantic data modeling

Copyright © 2015 Pearson Education, Inc.


4-18

You might also like