4. Relational Database
4. Relational Database
RELATIONAL DATABASES
CONTENTS:
4.1. FILES VS DATABASES
4.2. IMPORTANCE OF DATABASE SYSTEMS
4.3. DATABASE SYSEMS
4.4. RELATIONAL DATABASES
1
4.1. 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.
– Information about the attributes of an entity
(e.g., the student’s ID number and birth date)
are stored in fields.
– All the fields containing data about one entity
(e.g., one student) form a record.
2
– The example below shows the record for Artie
Moore.
STUDENTS
First Phone
Student ID Last Name 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
3
– 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.
STUDENTS
Student Class
File File
Advisor
File
5
• 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.
6
• This proliferation of master
files created problems:
– Often the same information
Master File 1
Fact A
Enrollment
Program
was stored in multiple master
Fact B files.
Fact C – Made it more difficult to
effectively integrate data and
Master File 2
obtain an organization-wide
Fact A Financial Aid view of the data.
Program
Fact D – Also, the same information
Fact F
may not have been consistent
between files.
Master File 1
Grades
• If a student changed his
Fact A Program phone number, it may have
Fact B been updated in one master
Fact F
file but not another. 7
Database • A database is a set of
Fact A Fact B
Fact C Fact D inter-related, centrally
Fact E Fact F coordinated files.
Database
Management
System
8
• The database approach
treats data as an
organizational resource
Database that should be used by
Fact A Fact B and managed for the
Fact C Fact D
Fact E Fact F
entire organization, not
just a particular
department.
Database
Management
• A database
System management system
(DBMS) serves as the
Enrollment Financial Aid Grades
interface between the
Program Program Program database and the
various application
programs.
9
Database • The combination of
Fact A Fact B
Fact C Fact D the database, the
Fact E Fact F
DBMS, and the
application programs
Database
Management that access the
System
database is referred to
as the database
Enrollment Financial Aid Grades
Program Program Program system.
10
• The person responsible
for the database is the
Database
Fact A Fact B database
Fact C Fact D administrator.
Fact E Fact F
• As technology improves,
Database
many large companies
Management are developing very
System
large databases called
data warehouses.
Enrollment Financial Aid Grades
Program Program Program
11
4.2. 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.
12
Database technology provides the following
benefits to organizations:
• Data integration:- Achieved by combining
master files into larger pools of data accessible
by many programs.
• Data sharing:-It’s easier to share data
• Reporting flexibility:-Reports can be revised easily
and generated as needed. The database can easily be
browsed to research problems or obtain detailed
information.
• Minimal data redundancy and inconsistencies:-
Because data items are usually stored only once.
13
• Data independence:-Data items are independent of the
programs that use them. Consequently, a data item can
be changed without changing the program and vice
versa. Hence, it makes programming easier and
simplifies data management.
• Central management of data:-Data management is
more efficient because the database administrator is
responsible for coordinating, controlling, and managing
data.
• Cross-functional analysis:-Relationships can be explicitly
defined and used in the preparation of management
reports. EXAMPLE: Relationship between selling costs
and promotional campaigns.
14
4.3. DATABASE SYSTEMS
• Logical and physical views of data:
– In file-oriented systems, programmers must
know the physical location and layout of records
used by a program.
• They must reference the location, length, and
format of every field they utilize.
• When data is used from several files, this process
becomes more complex.
– Database systems overcome this problem by
separating the storage and use of data
elements. 15
– Two separate views of the data are
provided:
• Logical view: -How the user or programmer
conceptually organizes and understands the
data.
• Physical view:-How and where the data are
physically arranged and stored.
– Separating these views facilitates
application development, because
programmers can focus on coding the
logic and not be concerned with storage
details. 16
Logical View—User A Logical View—User B
Scholarship Distribution
Enrollment by Class Fr.
5%
Sr. Soph.
33% 24%
Jr.
38%
Database
17
Logical View—User A Logical View—User B
Scholarship Distribution
Enrollment by Class Fr.
5%
Sr. Soph.
33% 24%
Jr.
38%
DBMS
The operating system
Operating translates DBMS
System
requests into
instructions to physically
retrieve data from
Database various disks.
18
• The DBMS handles the link between the physical
and logical views of the data.
– Allows the user to access, query, and update data
without reference to how or where it is physically
stored.
– The user only needs to define the logical data
requirements.
• Separating the logical and physical views of data
also means users can change their
conceptualizations of the data relationships
without making changes in the physical storage.
19
• The database administrator can also change the
physical storage of the data without affecting
users or application programs.
• Schemas-A schema describes the logical
structure of a database. There are three levels of
schema.
(1) Conceptual level:
• The organization-wide view of the entire
database—i.e., the big picture.
• Lists all data elements and the relationships
between them.
20
Subschema--User A Subschema--User B Subschema--User C
Smith . . . A
Jones . . . B
Arnold . . .D
Cash
Receipt
22
Subschema--User A Subschema--User B Subschema--User C
Smith . . . A
Jones . . . B
Arnold . . .D
Cash
Receipt
24
Subschema--User A Subschema--User B Subschema--User C
Smith . . . A
Jones . . . B
Arnold . . .D
Cash
Receipt
The
Classes Enroll Student bidirectional
arrows
represent
mappings
Cash
between the
Receipt
schema.
30
• Creating a database:
– The set of commands used to create the
database is known as data definition
language (DDL). DDL is used to:
• Build the data dictionary
• Initialize or create the database
• Describe the logical views for each individual user
or programmer
• Specify any limitations or constraints on security
imposed on database records or fields
31
• Changing a database
– The set of commands used to change the
database is known as data manipulation
language (DML). DML is used for
maintaining the data including:
• Updating data
• Inserting data
• Deleting portions of the database
32
• Querying a database:
– The set of commands used to query the
database is known as data query language
(DQL). DQL is used to interrogate the database,
including:
• Retrieving records
• Sorting records
• Ordering records
• Presenting subsets of the database
– The DQL usually contains easy-to-use, powerful
commands that enable users to satisfy their
own information needs.
33
• Report Writer
– Many DBMS packages also include a report
writer, a language that simplifies the creation
of reports.
– Users typically specify:
• What elements they want printed
• How the report should be formatted
– The report writer then:
• Searches the database
• Extracts specified data
• Prints them out according to specified format
34
• Users typically have access to both DQL and report
writer.
• Access to DQL and DML are typically restricted to
employees with administrative and programming
responsibilities.
35
4-4.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 (relations).
36
STUDENTS
Last First Phone
Student ID Name Name No.
333-33-3333 Simpson Alice 333-3333
Relation
111-11-1111 Sanders Ned 444-4444
123-45-6789 Moore Artie 555-5555
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 x COURSE
SCID Student ID Course
333333333-1234 333-33-3333 1234
333333333-1236 333-33-3333 1236
111111111-1235 111-11-1111 1235
111111111-1236 111-11-1111 1235
• This model only describes how the data appear
in the conceptual- and external-level schemas.
• The data are physically stored according to the
description in the internal-level schema.
38
STUDENTS
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
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
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 x COURSE
SCID
333333333-1234
In some tables, two or more
333333333-1236 attributes may be joined to form the
111111111-1235 primary key.
111111111-1236
STUDENTS
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
ADVISORS
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
ADVISORS
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
45
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
50
Last First Phone
Student ID Name Name No. Class 1 Class 2 Class 3 Class 4
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 x COURSE • The solution to the preceding problems is
SCID to use a set of tables in a relational
333333333-1234
database.
333333333-1236
• Each entity is stored in a separate table,
111111111-1235
111111111-1236 and separate tables or foreign keys can be
used to link the entities together.
• Basic requirements of a relational database:
(1) Every column in a row must be single valued.
• In other words, every cell can have one and only one
value.
(2) 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.
(3) 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.
53
STUDENTS
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
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 x COURSE
• Note that within each table, there are no
SCID
duplicate primary keys and no null
333333333-1234
333333333-1236 primary keys.
111111111-1235 • Consistent with the entity integrity rule.
111111111-1236
(4) All non-key attributes in a table should describe a
characteristic of the object identified by the primary key.
• The preceding four constraints produce a well-structured
(normalized) database in which:
– Data are consistent.
– Redundancy is minimized and controlled.
• In a normalized database, attributes appear multiple
times only when they function as foreign keys.
• The referential integrity rule ensures there will be no
update anomaly problem with foreign keys.
55
STUDENTS
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
ADVISORS
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
57
STUDENTS
• Add a
Last First Phone student
Student ID Name Name No. here.
333-33-3333 Simpson Alice 333-3333 • Leaves no
111-11-1111 Sanders Ned 444-4444
123-45-6789 Moore Artie 555-5555 blank 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 x COURSE
SCID • Add a course here.
333333333-1234 • Leaves no blank spaces.
333333333-1236
111111111-1235 • When a particular student enrolls for
111111111-1236 a particular course, add that info
• Deletion of a class for a student would cause
the elimination of one record in the student x
class table.
– The student still exists in the student table.
– The class still exists in the class table.
– Avoids the delete anomaly.
59
STUDENTS
Last First Phone • Ned still
Student ID Name Name No. exists in the
333-33-3333 Simpson Alice 333-3333 student
111-11-1111 Sanders Ned 444-4444 table.
123-45-6789 Moore Artie 555-5555
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 x COURSE
• Even if Ned was the only student in the
SCID
class, ACCT-3603 still exists in the course
333333333-1234
table.
333333333-1236
111111111-1235 • If Ned Sanders drops ACCT-3603, remove
111111111-1236 Ned’s class from this table.
• There are two basic ways to design well-
structured relational databases.
– Normalization
– Semantic data modeling
61
• 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.
62
• Semantic data modeling
– Database designer uses knowledge about how business
processes typically work and the information needs
associated with transaction processing to draw a
graphical picture of what should be included in the
database.
– The resulting graphic is used to create a set of
relational tables that are in 3NF.
63
• 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.
• Creating relational database queries
– Databases store data for people and organizations.
– To retrieve the data, you query the database and its
tables by using Microsoft Access in lab class.