• 沒有找到結果。

03-The Relational Database Model

N/A
N/A
Protected

Academic year: 2021

Share "03-The Relational Database Model"

Copied!
71
0
0

加載中.... (立即查看全文)

全文

(1)

3

Chapter 3

The Relational Database Model

Database Systems:

Design, Implementation, and Management,

Sixth Edition, Rob and Coronel

(2)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

2

In this chapter, you will learn:

•That the relational database model takes a

logical view of data

•That the relational model’

s basic components

are entities, attributes, and relationships

among entities

•How entities and their attributes are

organized into tables

(3)

3

In this chapter, you will learn (continued):

•About relational database operators, the data

dictionary, and the system catalog

•How data redundancy is handled in the

relational database model

(4)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

4

A Logical View of Data

•Relational model

–Enables us to view data logically rather than

physically

–Reminds us of simpler file concept of data

storage

•Table

–Has advantages of structural and data

independence

–Resembles a file from conceptual point of view

–Easier to understand than its hierarchical and

(5)

3

Tables and Their Characteristics

•Table: two-dimensional structure composed of

rows and columns

•Contains group of related entities an entity

set

–Terms entity set and table are often used

interchangeably

(6)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

6

Tables and Their Characteristics

(continued)

•Table also called a relation because the

relational model’

s creator, Codd, used the term

relation as a synonym for table

•Think of a table as a persistent relation:

–A relation whose contents can be permanently

saved for future use

(7)

3

Characteristics of a Relational Table

(8)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

8

STUDENT Table Attribute Values

(9)

3

Keys

•Consists of one or more attributes that

determine other attributes

•Primary key (PK) is an attribute (or a

combination of attributes) that uniquely

identifies any given entity (row)

•Key’

s role is based on determination

–If you know the value of attribute A, you can

look up (determine) the value of attribute B

(10)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

10

Student Classification

(11)

3

Keys (continued)

•Composite key

–Composed of more than one attribute

•Key attribute

–Any attribute that is part of a key

•Superkey

–Any key that uniquely identifies each entity

•Candidate key

(12)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

12

Null Values

•No data entry

•Not permitted in primary key

•Should be avoided in other attributes

•Can represent

–An unknown attribute value

–A known, but missing, attribute value

–A “

not applicable”condition

•Can create problems in logic and using

formulas

(13)

3

Controlled Redundancy

•Makes the relational database work

•Tables within the database share common

attributes that enable us to link tables

together

•Multiple occurrences of values in a table are

not redundant when they are required to

make the relationship work

•Redundancy is unnecessary duplication of

data

(14)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

14

An Example of a

(15)

3

The Relational Schema for the

CH03_SaleCo Database

(16)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

16

Keys (continued)

•Foreign key (FK)

–An attribute whose values match primary key

values in the related table

•Referential integrity

–FK contains a value that refers to an existing

valid tuple (row) in another relation

•Secondary key

(17)

3

(18)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

18

Integrity Rules

(19)

3

(20)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

20

A Dummy Variable Value Used as a Flag

(21)

3

Relational Database Operators

•Relational algebra

–Defines theoretical way of manipulating table

contents using relational operators:

•SELECT

•PROJECT

•JOIN

•INTERSECT

–Use of relational algebra operators on existing

tables (relations) produces new relations

•UNION

•DIFFERENCE

•PRODUCT

•DIVIDE

(22)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

22

Relational Algebra Operators (continued)

•Union:

–Combines all rows from two tables, excluding

duplicate rows

–Tables must have the same attribute

characteristics

•Intersect:

(23)

3

(24)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

24

Intersect

(25)

3

Relational Algebra Operators (continued)

•Difference

–Yields all rows in one table not found in the

other table—that is, it subtracts one table from

the other

•Product

–Yields all possible pairs of rows from two

tables

(26)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

26

Difference

(27)

3

(28)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

28

Relational Algebra Operators (continued)

•Select

–Yields values for all rows found in a table

–Can be used to list either all row values or it

can yield only those row values that match a

specified criterion

–Yields a horizontal subset of a table

•Project

–Yields all values for selected attributes

–Yields a vertical subset of a table

(29)

3

(30)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

30

Project

(31)

3

Relational Algebra Operators (continued)

•Join

–Allows us to combine information from two or

more tables

–Real power behind the relational database,

allowing the use of independent tables linked

by common attributes

(32)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

32

Two Tables That Will Be Used

(33)

3

Natural Join

• Links tables by selecting only rows with

common values in their common attribute(s)

• Result of a three-stage process:

1. PRODUCT of the tables is created

2. SELECT is performed on Step 1 output to

yield only the rows for which the

AGENT_CODE values are equal

• Common column(s) are called join column(s)

3. PROJECT is performed on Step 2 results to

yield a single copy of each attribute, thereby

eliminating duplicate columns

(34)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

34

Natural Join, Step 1: PRODUCT

(35)

3

(36)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

36

Natural Join, Step 3: PROJECT

(37)

3

Natural Join (continued)

•Final outcome yields table that

–Does not include unmatched pairs

–Provides only copies of matches

•If no match is made between the table rows,

–the new table does not include the unmatched

row

(38)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

38

Natural Join (continued)

•The column on which we made the JOIN—that

is, AGENT_CODE—occurs only once in the

new table

•If the same AGENT_CODE were to occur

several times in the AGENT table,

(39)

3

Other Forms of Join

•Equijoin

–Links tables on the basis of an equality

condition that compares specified columns of

each table

–Outcome does not eliminate duplicate

columns

–Condition or criterion to join tables must be

explicitly defined

–Takes its name from the equality comparison

operator (=) used in the condition

•Theta join

(40)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

40

Outer Join

•Matched pairs are retained and any

unmatched values in other table are left null

•In outer join for tables CUSTOMER and

AGENT, two scenarios are possible:

–Left outer join

•Yields all rows in CUSTOMER table, including

those that do not have a matching value in the

AGENT table

–Right outer join

•Yields all rows in AGENT table, including those

that do not have matching values in the

(41)

3

(42)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

42

Right Outer Join

(43)

3

Divide

•DIVIDE requires the use of one single-column

table and one two-column table

(44)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

44

DIVIDE

(45)

3

The Data Dictionary

and System Catalog

•Data dictionary

–Used to provide detailed accounting of all

tables found within the user/designer-created

database

–Contains (at least) all the attribute names and

characteristics for each table in the system

–Contains metadata—data about data

–Sometimes described as “

the database

designer’

s database”because it records the

design decisions about tables and their

(46)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

46

A Sample Data Dictionary

(47)

3

The Data Dictionary

and the System Catalog (continued)

•System catalog

–Contains metadata

–Detailed system data dictionary that describes

all objects within the database

–Terms “

system catalog”and “

data dictionary”

are often used interchangeably

–Can be queried just like any

user/designer-created table

(48)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

48

Relationships within the

Relational Database

•1:M relationship

–Relational modeling ideal

–Should be the norm in any relational database

design

•M:N relationships

–Must be avoided because they lead to data

redundancies

•1:1 relationship

–Should be rare in any relational database

design

(49)

3

The 1:1 Relationship

•Relational database norm

•Found in any database environment

•One entity can be related to only one other

entity, and vice versa

•Often means that entity components were not

defined properly

•Could indicate that two entities actually

belong in the same table

(50)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

50

The 1:1 Relationship Between

(51)

3

The Implemented 1:1 Relationship Between

PROFESSOR and DEPARTMENT

(52)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

52

The 1:M Relationship

(53)

3

The Implemented 1:M Relationship

Between PAINTER and PAINTING

(54)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

54

The 1:M Relationship

(55)

3

The Implemented 1:M Relationship

Between COURSE and CLASS

(56)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

56

The M:N Relationship

•Can be implemented by breaking it up to

produce a set of 1:M relationships

•Can avoid problems inherent to M:N

relationship by creating a composite entity or

bridge entity

(57)

3

The ERD’

s M:N Relationship

Between STUDENT and CLASS

(58)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

58

Sample Student Enrollment Data

(59)

3

The M:N Relationship

(60)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

60

Linking Table

•Implementation of a composite entity

•Yields required M:N to 1:M conversion

•Composite entity table must contain at least

the primary keys of original tables

•Linking table contains multiple occurrences of

the foreign key values

•Additional attributes may be assigned as

needed

(61)

3

Converting the M:N Relationship

into Two 1:M Relationships

(62)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

62

Changing the M:N Relationship

(63)

3

(64)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

64

The Relational Schema for the

(65)

3

Data Redundancy Revisited

•Data redundancy leads to data anomalies

–Such anomalies can destroy database

effectiveness

•Foreign keys

–Control data redundancies by using common

attributes shared by tables

–Crucial to exercising data redundancy control

(66)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

66

A Small Invoicing System

(67)

3

The Relational Schema

for the Invoicing System

(68)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

68

Indexes

•Arrangement used to logically access rows in

a table

•Index key

–Index’

s reference point

–Points to data location identified by the key

•Unique index

–Index in which the index key can only have

one pointer value (row) associated with it

(69)

3

(70)

Database Systems: Design, Implementation, & Management, 6thEdition, Rob & Coronel

3

70

Summary

•Entities are basic building blocks of a

relational database

•Entity set is a grouping of related entities,

stored in a table

•Keys define functional dependencies

–Superkey

–Candidate key

–Primary key

–Secondary key

–Foreign key

(71)

3

Summary (continued)

•Primary key uniquely identifies attributes

–Can link tables by using controlled redundancy

•Relational databases classified according to

degree to which they support relational algebra

functions

•Relationships between entities are represented

by entity relationship models

•Data retrieval speed can be increased

dramatically by using indexes

參考文獻

相關文件

QCD Soft Wall Model for the scalar scalar & & vector vector glueballs glueballs

代碼 姓名 姓別 住址 電話 部門 部門 位置..

[r]

Resources for the TEKLA curriculum at Junior Secondary Topic 6 Simple & Compound Interests and TVM Strategies and Management – Extension Learning Element1. Module E4

Using MS Access to design database, learning SQL commands and create forms and

Global Logistics and Supply Chain Management, 3rd Edition, John Wiley & Sons. Global Macrotrends and their Impact on Supply

The Performance Evaluation for Horizontal, Vertical and Hybrid Schema in Database Systems.. -A Case Study of Wireless Broadband

This research is focused on the integration of test theory, item response theory (IRT), network technology, and database management into an online adaptive test system developed