50
Copyright © 2015 Pearson Education, Inc. Relational Databases Chapter 4 4-1

Relational Databases

Embed Size (px)

DESCRIPTION

Relational Databases. Chapter 4. INTRODUCTION. Relational databases underlie most modern integrated AISs. They are the most popular type of database used for transaction processing. What Is a Database?. Efficiently and centrally coordinates information for a related group of files - PowerPoint PPT Presentation

Citation preview

Copyright © 2015 Pearson Education, Inc.

Relational DatabasesChapter 4

4-1

Copyright © 2015 Pearson Education, Inc.

INTRODUCTION

•Relational databases underlie most modern integrated AISs.▫They are the most popular type of database used for transaction processing.

4-2

Copyright © 2015 Pearson Education, Inc.

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)

4-3

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.

4-4

Copyright © 2015 Pearson Education, Inc.

FILE VS. DATABASES•This proliferation of

master files created problems:▫Often the same

information was stored in multiple master files.

▫Made it more difficult to effectively integrate data and obtain an organization-wide view of the data.

▫Also, the same information may not have been consistent between files. If a customer changed

their phone number, it may have been updated in one master file but not another.

Master File 1Fact AFact BFact CFact D

Master File 2Fact AFact CFact EFact F

Master File 3Fact AFact DFact EFact G

SalesProgram

ShippingProgram

BillingProgram

4-5

Copyright © 2015 Pearson Education, Inc.

FILE VS. DATABASES

•A database is a set of inter-related, centrally coordinated files.

DatabaseFact A Fact BFact C Fact DFact E Fact F Fact G

SalesProgram

ShippingProgram

BillingProgram

DatabaseManagement

System

4-6

Copyright © 2015 Pearson Education, Inc.

FILE VS. DATABASES•The database

approach treats data as an organizational resource that should be used by and managed for the entire organization, not just a particular department.

•A database management system (DBMS) serves as the interface between the database and the various application programs.

DatabaseFact A Fact BFact C Fact DFact E Fact F Fact G

SalesProgram

ShippingProgram

BillingProgram

DatabaseManagement

System

4-7

Copyright © 2015 Pearson Education, Inc.

FILE VS. DATABASES

•The combination of the database, the DBMS, and the application programs that access the database is referred to as the database system.

DatabaseFact A Fact BFact C Fact DFact E Fact F Fact G

ShippingProgram

BillingProgram

DatabaseManagement

System

SalesProgram

4-8

Copyright © 2015 Pearson Education, Inc.

FILE VS. DATABASES• The person responsible for the database is the database administrator.

• As technology improves, many large companies are developing very large databases called data warehouses.

• Data Mining Analysis to identify

relationships in the data, new knowledge about business processes, etc.

Example, Credit card issuers efforts to defect fraud

DatabaseFact A Fact BFact C Fact DFact E Fact F Fact G

ShippingProgram

BillingProgram

DatabaseManagement

System

SalesProgram

4-9

Copyright © 2015 Pearson Education, Inc.

FILE VS. DATABASES

Master File 1Fact AFact BFact CFact D

Master File 2Fact AFact CFact EFact F

Master File 3Fact AFact DFact EFact G

SalesProgram

ShippingProgram

BillingProgram

DatabaseFact A Fact BFact C Fact DFact E Fact F Fact G

ShippingProgram

BillingProgram

DatabaseManagement

System

SalesProgram

4-10

Copyright © 2015 Pearson Education, Inc.

Advantages of Database Systems

•Data Integration▫Files are logically combined and made

accessible to various systems.•Data Sharing

▫With data in one place it is more easily accessed by authorized users.

•Reporting flexibility: Reports can be revised easily and generated as needed and the database can be easily browsed to research a problem or obtain detailed information underlying a summary report

4-11

Copyright © 2015 Pearson Education, Inc.

Advantages of Database Systems

•Minimizing Data Redundancy and Data Inconsistency▫Eliminates the same data being stored

in multiple files, thus reducing inconsistency in multiple versions of the same data.

•Data Independence▫Data is separate from the programs

that access it. Changes can be made to the data without necessitating a change in the programs and vice versa

4-12

Copyright © 2015 Pearson Education, Inc.

Advantages of Database Systems

•Central management of data: ▫Data management is more efficient

because a database administrator is responsible for coordinating, controlling and managing data

•Cross-Functional Analysis▫Relationships between data from various organizational departments can be more easily combined. Example: association between sales and promotional campaigns

4-13

Copyright © 2015 Pearson Education, Inc.

Advantages of Database Systems

•One-time Data Entry and Storage: ▫In the data-base approach to data

management data are input into the data base once, stored in a particular location and available for use my multiple applications and users

4-14

Copyright © 2015 Pearson Education, Inc.

IMPORTANCE AND ADVANTAGES OF DATABASE SYSTEMS

•The importance of good data:▫Bad data leads to:

Bad decisions Embarrassment Angry users

4-15

Copyright © 2015 Pearson Education, Inc.

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.

4-16

Copyright © 2015 Pearson Education, Inc.

Physical View

•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.

4-17

Copyright © 2015 Pearson Education, Inc.

Physical View

4-18

Copyright © 2015 Pearson Education, Inc.

4-19

Copyright © 2015 Pearson Education, Inc.

Schemas

4-20

• Describe the logical structure of a database▫Conceptual Level

Organization wide view of the data

▫External Level Individual users view of

the data Each view is a

subschema▫Internal Level

Describes how data are stored and accessed Description of: records,

definitions, addresses, and indexes

4-20

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.

4-21

Copyright © 2015 Pearson Education, Inc.

The Data Dictionary

4-22

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•Report Writer

▫Simplifies report creation4-23

Copyright © 2015 Pearson Education, Inc.

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.

4-24

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

4-25

Copyright © 2015 Pearson Education, Inc.

Types of Attributes•Primary key

▫ is the attribute, or combination of attributes, that uniquely identifies a specific row(record) in a table.

• Foreign key▫is an attribute in a table that is a

primary key in another table. ▫Foreign keys are used to link tables.

•Other Non Key Attributes▫Store other important data about the

entity

4-26

Copyright © 2015 Pearson Education, Inc.

Relational Data Tables

4-26

Invoice # is PK Customer # is PK

Item # is PK

Combination of Invoice # & Item # forms the PK

Copyright © 2015 Pearson Education, Inc.

Relational Data Tables

4-28

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.

Why Have a Set of Related Tables?•Data stored in one large table can be redundant and inefficient causing the following problems:▫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. 4-29

Copyright © 2015 Pearson Education, Inc.

Database Design Errors

•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 at a University, we could store all data in one table and have a separate line for each student x class combination.

4-30

Copyright © 2015 Pearson Education, Inc.

Student IDLast

NameFirst 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.

4-31

Copyright © 2015 Pearson Education, Inc.

Student IDLast

NameFirst 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

• 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.4-32

Copyright © 2015 Pearson Education, Inc.

Student IDLast

NameFirst 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

• 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.

4-33

Copyright © 2015 Pearson Education, Inc.

Student IDLast

NameFirst 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

• 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.4-34

Copyright © 2015 Pearson Education, Inc.

Database Design Errors

•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.

4-35

Copyright © 2015 Pearson Education, Inc.

• This approach is also fraught with problems:– How many classes should you allow 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.

Student IDLast

NameFirst Name

Phone 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    

4-36

Copyright © 2015 Pearson Education, Inc.

Student IDLast

NameFirst

NamePhone

No.333-33-3333 Simpson Alice 333-3333111-11-1111 Sanders Ned 444-4444123-45-6789 Moore Artie 555-5555

STUDENTS

Course ID Course Section Day Time1234 ACCT-3603 1 MWF 8:301235 ACCT-3603 2 TR 9:301236 MGMT-2103 1 MW 8:30

COURSES

• The solution to the preceding problems is to use a set of tables in a relational database.

• Each entity is stored in a separate table, and separate tables or foreign keys can be used to link the entities together.

Student x CourseStudent ID Course ID333333333 1234333333333 1236111111111 1235111111111 1236

4-37

Copyright © 2015 Pearson Education, Inc.

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. 4-38

STUDENT COURSETAKES

(1,N) (1,N)

student #

name

address

course #

name

Student# Name Address

1 Tony Cleveland

2 Emily New York

3 Leigh Birmingham

Course# Name

Acg4401 AIS

Acg3101 FAR 1

Course# *

Acg4401, Acg3101

Acg4401, Acg3101

Acg4401, Acg3101

Student#*

1, 2, 3

1, 2, 3

Basic requirements of a relational database• Every column(field) in a row must be single

valued.• In other words, every cell can have one and only one

value.

4-39

Copyright © 2015 Pearson Education, Inc.

STUDENT COURSETAKES

(1,N) (1,N)

student #

name

address

course #

name

Student# Name Address

1 Tony Cleveland

2 Emily New York

3 Leigh Birmingham

Course# Name

Acg4401 AIS

Acg3101 FAR 1

Student# Course#

1 Acg4401

1 Acg3101

2 Acg4401

2 Acg3101

3 Acg3101

Relationship Table

4-40

• Note that within each table, there are no duplicate primary keys and no null primary keys.

• Consistent with the entity integrity rule.

FK example

Not in Salesperson Table

Referential integrity would prevent this from happening 4-

41

Copyright © 2015 Pearson Education, Inc.

RELATIONAL DATABASES•An important feature is that data about

various things of interest (entities) are stored in separate tables.▫Makes it easier to add new data to the system.

You add a new student by adding a row to the student table.

You add a new course by adding a row to the course table.

Means you can add a student even if he hasn’t signed up for any courses.

And you can add a class even if no students are yet enrolled in it.

▫Makes it easy to avoid the insert anomaly.•Space is also used more efficiently than in

the other schemes. There should be no blank rows or attributes. 4-

42

Copyright © 2015 Pearson Education, Inc.

Student IDLast

NameFirst

NamePhone

No.333-33-3333 Simpson Alice 333-3333111-11-1111 Sanders Ned 444-4444123-45-6789 Moore Artie 555-5555

STUDENTS

Course ID Course Section Day Time1234 ACCT-3603 1 MWF 8:301235 ACCT-3603 2 TR 9:301236 MGMT-2103 1 MW 8:30

COURSES

• Add a student here.

• Leaves no blank spaces.

• Add a course here.• Leaves no blank

spaces.• When a particular student enrolls for

a particular course, add that info here.

Student x CourseStudent ID Course ID333333333 1234333333333 1236111111111 1235111111111 1236

4-43

Copyright © 2015 Pearson Education, Inc.

RELATIONAL DATABASES

•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.

4-44

Copyright © 2015 Pearson Education, Inc.

Student IDLast

NameFirst

NamePhone

No.333-33-3333 Simpson Alice 333-3333111-11-1111 Sanders Ned 444-4444123-45-6789 Moore Artie 555-5555

STUDENTS

Course ID Course Section Day Time1234 ACCT-3603 1 MWF 8:301235 ACCT-3603 2 TR 9:301236 MGMT-2103 1 MW 8:30

COURSES

• Ned still exists in the student table.

• Even if Ned was the only student in the class, ACCT-3603 still exists in the course table.

• If Ned Sanders drops ACCT-3603, remove Ned’s class from this table.

Student x CourseStudent ID Course ID333333333 1234333333333 1236111111111 1235111111111 1236

4-45

Copyright © 2015 Pearson Education, Inc.

Database Design

•There are two basic ways to design well-structured relational databases.▫Normalization▫Semantic data modeling(chapter 17)

4-46

Copyright © 2015 Pearson Education, Inc.

Normalizing Relational Databases

•Initially, one table is used for all the data in a database.

•Following rules, the table is decomposed into multiple tables related by:▫Primary key–foreign key integration

•Decomposed set of tables are in third normal form (3NF).

4-47

Table 4-14 page 103 4-48

Borrower Number

Last Name

First Name

Current Address

Requested Mortgage Amount

Lender Number

Lender Name

Lender Office Address

Property Appraiser Number

Property Appraiser Name

450 Adams Jennifer 450 $245,000 13 Excel 6890 8 Advent

Peachtree Rd. Mortgage Sheridan Dr. Appraisers

451 Adamson David 500 Loop $124,688 I3 Excel 6890 9 Independent

Highway Mortgage Sheridan Dr. Appraisal

Service

452 Bronson Paul 312 Mountain $345,000 14 CCY 28 Buckhead 10 Jones

View Dr. Way Property

Appraisers

453 Brown Marietta 310 Loop $ 57,090 15 Advantage 3345 Lake 10 Jones

Highway Lenders Shore Dr. Property

Appraisers

454 Charles Kenneth 3 Commons $ 34,000 16 Capital 8890 Coral 8 Advent

Blvd. Savings Blvd. - Appraisers

455 Coulter Tracey 1367 $216,505 13 Excel 6890 8 Advent

Peachtree Rd. Mortgage Sheridan Dr. Appraisers

456 Foster Harold 678 Loop $117,090 12 National 750 16 St. 9 Independent

Highway Mortgage Appraisal

Service

457 Frank Vernon 210 Bicayne $ 89,000 12 National 750 16 St. 10 Jones

Blvd. Mortgage Property

Appraisers

458 Holmes Heather 1121 Bicayne $459,010 16 Capital 8890 Coral 10 Jones

Blvd. Savings Blvd. Property

Appraisers

459 Johanson Sandy 817 Mountain $ 67,900 15 Advantage 3345 Lake 9 Independent

View Dr. Lenders Shore Dr. Appraisal

Service

460 Johnson James 985 Loop ' $ 12,000 12 National 750 16 St. 10 Jones

Highway Mortgage Property

Appraisers

461 Jones Holly 1650 $ 67,890 15 Advantage 3345 Lake 9 Independent

Washington Lenders Shore Dr. Appraisal

Blvd. Service

FK FK

49

Lender Number Lender Name Lender Address12 National Mortgage 750 16 St.13 Excel Mortgage 6890 Sheridan Dr. 14 CCY 28 Buckhead Way 15 Advantage Lenders 3345 Lake Shore Dr.16 Capital Savings 8890 Coral Blvd.

Borrower Number Last

NameFirst Name Current Address

Requested Mortgage Amount

Lender Number

Property Appraiser Number

450 Adams Jennifer 450 Peachtree Rd. $245,000 13 8451 Adamson David 500 Loop Highway $124,688 13 9452 Bronson Paul 312 Mountain View Dr. $345,000 14 10453 Brown Marietta 310 Loop Highway $57,090 15 10454 Charles Kenneth 3 Commons Blvd. $34,000 16 8455 Coulter Tracey 1367 Peachtree Rd. $216,505 13 8456 Foster Harold 678 Loop Highway $117,090 12 9457 Frank Vernon 210 Bicayne Blvd. $89,000 12 10458 Holmes Heather 1121 Bicayne Blvd. $459,010 16 10459 Johanson Sandy 817 Mountain View Dr. $67,900 15 9460 Johnson James 985 Loop Highway $12,000 12 10461 Jones Holly 1650 Washington Blvd. $67,890 15 9

Property Appraiser Number

Property Appraiser Name

8 Advent Appraisers

9 Independent Appraisal Service

10 Jones Property Appraisers

Solution page 112

Copyright © 2015 Pearson Education, Inc.

Primary key/Foreign Key•Establishing relationship•Primary key/Foreign key

must be like type data types▫Auto# cannot be FK

because it can only be used one time

Data TypePK FK

auto# Auto# NOAuto# Number Yes

Number Auto# NONumber Number YesNumber Text NO

Text Text YesText Number NO

50