3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model...

67
3 The Relational Model MIS 304 Winter 2006

Transcript of 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model...

Page 1: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

3

The Relational Model

MIS 304 Winter 2006

Page 2: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

2

3

Class Objectives

• 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

• About relational database operators, the data dictionary, and the system catalog

• How data redundancy is handled in the relational database model

Page 3: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

3

3A Logical View of Data

• Still trying to free ourselves form the physical implementation problems.

• 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 network database predecessors

Page 4: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

4

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

Page 5: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

5

3

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.

Page 6: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

6

3

Characteristics of a Relational Table

Table 3.1

Page 7: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

7

3STUDENT Table Attribute Values

Page 8: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

8

3Keys

• 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

– This extends to the notion of a mathematical “function” f(x).

Page 9: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

9

3Keys (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

– A superkey without redundancies

Page 10: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

10

3Null 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

Page 11: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

11

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

Page 12: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

12

3An Example of a

Simple Relational Database

Page 13: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

13

3

The Relational Schema for the CH03_SaleCo Database

Page 14: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

14

3

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

– Key used strictly for data retrieval purposes

Page 15: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

15

3Relational Database Keys

Page 16: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

16

3

Types of Participation

• Mandatory– Table A’s participation is mandatory if you

must enter at least one record in Table A before you can enter values in Table B

• Optional– Table A’s participation is optional if you

are not required enter at least one record in Table A before you can enter values in Table B

Page 17: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

17

3

Degree of Participation

• Is the minimum and maximum number of records (entity instances) a Table must have associated with a single record in the related Table.

• Usually expressed as a pair of numbers Min,Max example 1,10.

Page 18: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

18

3

Integrity Rules

Page 19: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

19

3

Integrity

• Table Level Integrity = Entity Integrity• Relationship Level Integrity = Referential

Integrity

• Field Level Integrity = Domain Integrity– Ensures that every field is sound. The

values are valid, consistent, and accurate

Page 20: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

20

3An Illustration of Integrity Rules

Page 21: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

21

3

Relational Algebra

• Codd’s contribution included the idea that you could describe an “Algebra”, a consistent mathematical description of a DBMS.

• This is huge because if it is ‘mathematically consistent’ then when you perform an operation you know that it must return the results you expect.

Page 22: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

22

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

Page 23: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

23

3Relational Algebra Operators

(continued)

• Union:

– Combines all rows from two tables, excluding duplicate rows

– Tables must have the same attribute characteristics

• Intersect:

– Yields only the rows that appear in both tables

Page 24: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

24

3

Page 25: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

25

3Relational 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

• Divide

– Divide one table by the attributes of another

– Seldom used

Page 26: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

26

3

Page 27: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

27

3Product

Yields all possible pairs of rows from two tables Also known as the Cartesian product

Page 28: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

28

3Relational 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

Page 29: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

29

3

Select

Page 30: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

30

3

Project

Page 31: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

31

3Relational 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

Page 32: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

32

3

Two Tables That Will Be Used in Join Illustrations

Page 33: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

33

3Natural 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 created2. 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

Page 34: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

34

3

Natural Join, Step 1: PRODUCT

Page 35: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

35

3

Natural Join, Step 2: SELECT

Page 36: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

36

3

Natural Join, Step 3: PROJECT

Page 37: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

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

Page 38: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

38

3

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,

– a customer would be listed for each match

Page 39: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

39

3

This is IT

• This is what makes the relational database work in practical terms.

• You can use values from different but related tables work together to get the results you need.

Page 40: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

40

3Other 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– If any other comparison operator is used

Page 41: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

41

3Outer 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 CUSTOMER table

Page 42: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

42

3

Left Outer Join

Page 43: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

43

3

Right Outer Join

Page 44: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

44

3The 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 structures

Page 45: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

45

3A Sample Data Dictionary

Page 46: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

46

3The 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

Page 47: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

47

3Relationships 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

Page 48: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

48

3The 1:1 Relationship

• 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

• Sometimes 1:1 relationships are appropriate

Page 49: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

49

3

The 1:1 Relationship Between PROFESSOR and DEPARTMENT

Page 50: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

50

3The Implemented 1:1 Relationship Between PROFESSOR and

DEPARTMENT

Page 51: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

51

3The 1:M Relationship Between PAINTER and PAINTING

Page 52: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

52

3The Implemented 1:M Relationship Between PAINTER and PAINTING

Page 53: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

53

3The 1:M Relationship

Between COURSE and CLASS

Page 54: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

54

3The Implemented 1:M Relationship

Between COURSE and CLASS

Page 55: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

55

3

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 called a bridge or linking entity

Page 56: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

56

3The ERD’s M:N Relationship

Between STUDENT and CLASS

Page 57: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

57

3

Sample Student Enrollment Data

Page 58: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

58

3The M:N Relationship Between STUDENT and CLASS

Page 59: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

59

3

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

Page 60: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

60

3Converting the M:N Relationship into Two 1:M Relationships

Page 61: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

61

3Changing the M:N Relationship to Two 1:M Relationships

Page 62: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

62

3The Expanded Entity Relationship Model

Page 63: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

63

3The Relational Schema for the Ch03_TinyCollege Database

Page 64: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

64

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

• Sometimes, data redundancy is necessary

Page 65: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

65

3A Small Invoicing System

Page 66: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

66

3The Relational Schema

for the Invoicing System

Page 67: 3 The Relational Model MIS 304 Winter 2006. 3 2 Class Objectives That the relational database model takes a logical view of data That the relational model’s.

67

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