Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Jet Database Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Concurrency control wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Database wikipedia , lookup

Functional Database Model wikipedia , lookup

Clusterpoint wikipedia , lookup

ContactPoint wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
Relational Databases
Chapter 4
Copyright © 2015 Pearson Education, Inc.
4-1
INTRODUCTION
• Relational databases underlie most
modern integrated AISs.
▫ They are the most popular type of
database used for transaction
processing.
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
• 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.
4-4
FILE VS. DATABASES
Master File 1
Fact A
Fact B
Fact C
Fact D
Master File 2
Fact A
Fact C
Fact E
Fact F
Sales
Program
Shipping
Program
Billing
Master File 3
Program
Fact A
Fact D
Fact E
Copyright © 2015 Pearson Education, Inc.
Fact G
• 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.
4-5
FILE VS. DATABASES
• A database is a set of
inter-related, centrally
coordinated files.
Database
Fact A Fact B
Fact C Fact D
Fact E Fact F
Fact G
Database
Management
System
Sales
Program
Shipping
Program
Copyright © 2015 Pearson Education, Inc.
Billing
Program
4-6
FILE VS. DATABASES
Database
Fact A Fact B
Fact C Fact D
Fact E Fact F
Fact G
Database
Management
System
Sales
Program
Shipping
Program
Copyright © 2015 Pearson Education, Inc.
Billing
Program
• 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.
4-7
FILE VS. DATABASES
Database
Fact A Fact B
Fact C Fact D
Fact E Fact F
Fact G
Database
Management
System
Sales
Program
Shipping
Program
Copyright © 2015 Pearson Education, Inc.
Billing
Program
• The combination of
the database, the
DBMS, and the
application
programs that
access the database
is referred to as the
database
system.
4-8
FILE VS. DATABASES
Database
Fact A Fact B
Fact C Fact D
Fact E Fact F
Fact G
Database
Management
System
Sales
Program
Shipping
Program
Copyright © 2015 Pearson Education, Inc.
Billing
Program
• 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
4-9
FILE VS. DATABASES
Master File 1
Fact A
Fact B
Fact C
Fact D
Master File 2
Fact A
Fact C
Fact E
Fact F
Sales
Program
Database
Fact A Fact B
Fact C Fact D
Fact E Fact F
Fact G
Shipping
Program
Database
Management
System
Billing
Master File 3
Program
Fact A
Fact D
Fact E
Copyright © 2015 Pearson Education, Inc.
Fact G
Sales
Program
Shipping
Program
Billing
Program
4-10
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
Copyright © 2015 Pearson Education, Inc.
4-11
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
Copyright © 2015 Pearson Education, Inc.
4-12
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
Copyright © 2015 Pearson Education, Inc.
4-13
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
Copyright © 2015 Pearson Education, Inc.
4-14
IMPORTANCE AND ADVANTAGES OF DATABASE
SYSTEMS
• The importance of good data:
▫ Bad data leads to:
 Bad decisions
 Embarrassment
 Angry users
Copyright © 2015 Pearson Education, Inc.
4-15
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-16
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.
Copyright © 2015 Pearson Education, Inc.
4-17
Physical View
Copyright © 2015 Pearson Education, Inc.
4-18
Copyright © 2015 Pearson Education, Inc.
4-19
Schemas
• 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
Copyright © 2015 Pearson Education, Inc.
4-20
4-20
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-21
4-22
The Data Dictionary
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 creation
Copyright © 2015 Pearson Education, Inc.
4-23
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-24
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-25
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
Copyright © 2015 Pearson Education, Inc.
4-26
Relational Data Tables
Invoice # is PK
Customer # is PK
Item # is PK
Combination of
Invoice # & Item #
forms the PK
Copyright © 2015 Pearson Education, Inc.
4-26
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-28
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.
Copyright © 2015 Pearson Education, Inc.
4-29
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.
Copyright © 2015 Pearson Education, Inc.
4-30
Student ID
Last
Name
First
Name
Phone No.
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
Course No.
Section
Day
Time
• 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.
4-31
Student ID
Last
Name
First
Name
Phone No.
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
Course No.
Section
Day
Time
• 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.
4-32
Student ID
Last
Name
First
Name
Phone No.
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
Course No.
Section
Day
Time
• 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.
4-33
Student ID
Last
Name
First
Name
Phone No.
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
Course No.
Section
Day
Time
• 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.
4-34
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.
Copyright © 2015 Pearson Education, Inc.
4-35
Student ID
Last
Name
First
Name
Phone
No.
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
Class 1
Class 2
Class 3
Class 4
• 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 1hour 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.
4-36
Student ID
333-33-3333
111-11-1111
123-45-6789
STUDENTS
Last
First
Name
Name
Simpson Alice
Sanders Ned
Moore
Artie
Course ID Course
1234
ACCT-3603
1235
ACCT-3603
1236
MGMT-2103
Student x Course
Student ID Course ID
1234
333333333
1236
333333333
1235
111111111
1236
111111111
Copyright © 2015 Pearson Education, Inc.
COURSES
Section
1
2
1
Phone
No.
333-3333
444-4444
555-5555
Day
MWF
TR
MW
Time
8:30
9:30
8:30
• 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.
4-37
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-38
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.
student #
course #
name
name
address
(1,N)
STUDENT
(1,N)
TAKES
COURSE
Student#
Name
Address
Course# *
1
Tony
Cleveland
Acg4401, Acg3101
2
Emily
New York
Acg4401, Acg3101
3
Leigh
Birmingham
Acg4401, Acg3101
Course#
Name
Student#*
Acg4401
AIS
1, 2, 3
Acg3101
FAR 1
1, 2, 3
4-39
• Note that within each table, there are no duplicate
primary keys and no null primary keys.
• Consistent with the entity integrity rule.
student #
course #
name
name
address
(1,N)
STUDENT
(1,N)
COURSE
TAKES
Student#
Name
Address
Student#
Course#
1
Tony
Cleveland
1
Acg4401
2
Emily
New York
1
Acg3101
3
Leigh
Birmingham
2
Acg4401
Course#
Name
2
Acg3101
Acg4401
AIS
3
Acg3101
Acg3101
FAR 1
Relationship Table
Copyright © 2015 Pearson Education, Inc.
4-40
FK example
Not in Salesperson Table
Referential integrity would prevent
this from happening
4-41
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.
Copyright © 2015 Pearson Education, Inc.
4-42
Student ID
333-33-3333
111-11-1111
123-45-6789
STUDENTS
Last
First
Name
Name
Simpson Alice
Sanders Ned
Moore
Artie
Course ID Course
1234
ACCT-3603
1235
ACCT-3603
1236
MGMT-2103
Student x Course
Student ID Course ID
1234
333333333
1236
333333333
1235
111111111
1236
111111111
Copyright © 2015 Pearson Education, Inc.
Phone
No.
333-3333
444-4444
555-5555
COURSES
Section
1
2
1
Day
MWF
TR
MW
• Add a
student
here.
• Leaves no
blank
spaces.
Time
8:30
9:30
8:30
• Add a course here.
• Leaves no blank spaces.
• When a particular student enrolls for a
particular course, add that info here.
4-43
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.
Copyright © 2015 Pearson Education, Inc.
4-44
Student ID
333-33-3333
111-11-1111
123-45-6789
STUDENTS
Last
First
Name
Name
Simpson Alice
Sanders Ned
Moore
Artie
Course ID Course
1234
ACCT-3603
1235
ACCT-3603
1236
MGMT-2103
Student x Course
Student ID Course ID
1234
333333333
1236
333333333
1235
111111111
1236
111111111
Copyright © 2015 Pearson Education, Inc.
Phone
No.
333-3333
444-4444
555-5555
COURSES
Section
1
2
1
Day
MWF
TR
MW
• Ned still
exists in
the student
table.
Time
8:30
9:30
8:30
• 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.
4-45
Database Design
• There are two basic ways to design wellstructured relational databases.
▫ Normalization
▫ Semantic data modeling(chapter 17)
Copyright © 2015 Pearson Education, Inc.
4-46
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).
Copyright © 2015 Pearson Education, Inc.
4-47
Table 4-14 page 103
FK
Requested
Mortgage
Amount
Lender
Number
450
Peachtree Rd.
$245,000
David
500 Loop
Highway
Bronson
Paul
453
Brown
454
4-48
FK
Lender
Name
Lender
Office
Address
13
Excel
Mortgage
6890
Sheridan Dr.
8
Advent
Appraisers
$124,688
I3
Excel
Mortgage
6890
Sheridan Dr.
9
Independent
Appraisal
Service
312 Mountain
View Dr.
$345,000
14
CCY
28 Buckhead
Way
10
Jones
Property
Appraisers
Marietta
310 Loop
Highway
$ 57,090
15
Advantage
Lenders
3345 Lake
Shore Dr.
10
Jones
Property
Appraisers
Charles
Kenneth
3 Commons
Blvd.
$ 34,000
16
Capital
Savings
8890 Coral
Blvd. -
8
Advent
Appraisers
455
Coulter
Tracey
1367
Peachtree Rd.
$216,505
13
Excel
Mortgage
6890
Sheridan Dr.
8
Advent
Appraisers
456
Foster
Harold
678 Loop
Highway
$117,090
12
National
Mortgage
750 16 St.
9
Independent
Appraisal
Service
457
Frank
Vernon
210 Bicayne
Blvd.
$ 89,000
12
National
Mortgage
750 16 St.
10
458
Holmes
Heather
1121 Bicayne
Blvd.
$459,010
16
Capital
Savings
8890 Coral
Blvd.
10
459
Johanson
Sandy
817 Mountain
View Dr.
$ 67,900
15
Advantage
Lenders
3345 Lake
Shore Dr.
9
460
Johnson
James
985 Loop
Highway
$ 12,000
12
National
Mortgage
750 16 St.
10
461
Jones
Holly
1650
Washington
Blvd.
$ 67,890
15
Advantage
Lenders
3345 Lake
Shore Dr.
9
Borrower
Number
Last
Name
First
Name
Current
Address
450
Adams
Jennifer
451
Adamson
452
'
Property
Appraiser
Number
Property
Appraiser
Name
Jones
Property
Appraisers
Jones
Property
Appraisers
Independent
Appraisal
Service
Jones
Property
Appraisers
Independent
Appraisal
Service
Solution page 112
Borrower
Number
450
451
452
453
454
455
456
457
458
459
460
461
Lender Number
12
13
14
15
16
Last
Name
Adams
Adamson
Bronson
Brown
Charles
Coulter
Foster
Frank
Holmes
Johanson
Johnson
Jones
First
Name
Jennifer
David
Paul
Marietta
Kenneth
Tracey
Harold
Vernon
Heather
Sandy
James
Holly
Lender Name
National Mortgage
Excel Mortgage
CCY
Advantage Lenders
Capital Savings
Requested
Mortgage
Current Address
Amount
450 Peachtree Rd.
$245,000
500 Loop Highway
$124,688
312 Mountain View Dr.
$345,000
310 Loop Highway
$57,090
3 Commons Blvd.
$34,000
1367 Peachtree Rd.
$216,505
678 Loop Highway
$117,090
210 Bicayne Blvd.
$89,000
1121 Bicayne Blvd.
$459,010
817 Mountain View Dr.
$67,900
985 Loop Highway
$12,000
1650 Washington Blvd.
$67,890
Lender Address
750 16 St.
6890 Sheridan Dr.
28 Buckhead Way
3345 Lake Shore Dr.
8890 Coral Blvd.
Lender Property
Number Appraiser
Number
13
8
13
9
14
10
15
10
16
8
13
8
12
9
12
10
16
10
15
9
12
10
15
9
Property Appraiser
Number
8
9
10
Property Appraiser Name
Advent Appraisers
Independent Appraisal Service
Jones Property Appraisers
49
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
Copyright © 2015 Pearson Education, Inc.
Data Type
PK
FK
auto#
Auto# NO
Auto# Number Yes
Number Auto# NO
Number Number Yes
Number
Text
NO
Text
Text
Yes
Text
Number NO
50