32
IM ISU 1 Database Fundamentals of Database Sys tems Chapter 5 The Relational Data Model

DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

Embed Size (px)

Citation preview

Page 1: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 1Database

Fundamentals of Database Systems

Chapter 5

The Relational Data Model

Page 2: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 2Database

Chapter Outline

Relational Model Concepts Relational Model Constraints and Relational

Database Schemas Update Operations and Dealing with

Constraint Violations

Page 3: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 3Database

Relational Model Concepts Basis of the model

The relational Model of Data is based on the concept of a Relation

» The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations

We review the essentials of the formal relational model in this chapter

In practice, there is a standard model based on SQL – this is described in Chapters 8 and 9

Page 4: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 4Database

Relational Model Concepts (cont.)

A Relation is a mathematical concept based on the ideas of sets

The model was first proposed by Dr. E.F. Codd of IBM in 1970» "A Relational Model for Large Shared Data

Banks," Communications of the ACM, June 1970

The above paper caused a major revolution in the field of database management and earned Dr. Codd the coveted ACM Turing Award

Page 5: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 5Database

Relational Model Concepts (cont.) Informal Definitions

RELATION: A table of values» A relation may be thought of as a set of rows» A relation may alternately be though of as a set

of columns» Each row of the relation may be given an

identifier» Each column typically is called by its column

name or column header or attribute name

Page 6: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 6Database

Relational Model Concepts (cont.) Formal Definitions

The Schema of a Relation R (A1, A2, .....An)

» Relation schema R is defined over attributes A1,

A2, .....An

» Each attribute Ai is the name of a role played by some domain D in the relation schema R

» e.g., A relation schema

STUDENT(Name, SSN, HomePhone, Address, OfficePhone, Age, GPA)

Page 7: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 7Database

Relational Model Concepts (cont.) A relation r(R) is a subset of the Cartesian pr

oduct of the domains that define R» r(R) (dom (A1) dom (A2) .... dom(An))

» e.g., Let S1 = {0,1}, S2 = {a,b,c}

r(R) = {<0, a>, <0, b>, <1, c>} A relation may be regarded as a set of tuples

(rows)» r = {t1, t2, . . ., tm}. » A tuple is an ordered set of values

t = <v1, v2, . . ., vn>

Page 8: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 8Database

Relational Model Concepts (cont.) R is also called the intension of a relation r is also called the extension of a relation The degree of a relation is the number of

attributes n of its relation schema

Page 9: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 9Database

Relational Model Concepts (cont.)

Page 10: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 10Database

Relational Model Concepts (cont.) Definition summary

Informal Terms Formal Terms

Table Relation

Column Attribute/Domain

Row Tuple

Values in a column Domain

Table Definition Schema of Relation

Populated Table Extension

Page 11: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 11Database

Characteristics of Relations Ordering of tuples in a relation r(R)

The tuples are not considered to be ordered, even though they appear to be in the tabular form

Page 12: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 12Database

Characteristics of Relations (cont.) Ordering of attributes in a relation schema R

We will consider the attributes in R(A1, A2, ..., A

n)

and the values in t=<v1, v2, ..., vn> to be ordered A more general alternative definition of relation does not require this ordering

Page 13: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 13Database

Characteristics of Relations (cont.) Values in a tuple

All values are considered atomic (indivisible) A special null value is used to represent values t

hat are unknown or inapplicable to certain tuples» e.g., OfficePhone for some students

We refer to component values of a tuple t by t[Ai] = vi (the value of attribute Ai for tuple t)

Similarly, t[Au, Av, ..., Aw] refers to the subtuple of t containing the values of attributes Au, Av, ..., Aw, respectively

Page 14: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 14Database

Relational Integrity Constraints Constraints are conditions that must hold

on all valid relation instances Three main types of constraints:

Key constraints Entity integrity constraints Referential integrity constraints

Page 15: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 15Database

Relational Integrity Constraints (cont.)

Key Constraints Superkey of R

» A set of attributes SK of R such that no two tuples in any valid relation instance r(R) will have the same value for SK, i.e., for any distinct tuples t1 and t2 in r(R), t1[SK] <> t2[SK]

Key of R» A minimal superkey» A superkey K such that removal of any attribute f

rom K results in a set of attributes that is not a superkey

Page 16: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 16Database

Relational Integrity Constraints (cont.)

» If a relation has several candidate keys, one is chosen arbitrarily to be the primary key

» The primary key attributes are underlined

» e.g,

CAR(State, Reg#, SerialNo, Make, Model, Year)

two keys, Key1 = {State, Reg#},

Key2 = {SerialNo}

{SerialNo, Make} is a superkey but not a key

Page 17: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 17Database

Relational Integrity Constraints (cont.)

Page 18: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 18Database

Relational Integrity Constraints (cont.)

Entity Integrity Relational database schema

» A set S of relation schemas that belong to the same database

» S is the name of the database, S = {R1, R2, ..., Rn}

A relational database state DB of S » A set of relation states DB = {r1, r2, . . ., rm}

such that each ri is a state of Ri

Page 19: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 19Database

Relational Integrity Constraints (cont.)

Page 20: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 20Database

Relational Integrity Constraints (cont.)

Page 21: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 21Database

Relational Integrity Constraints (cont.)

Page 22: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 22Database

Relational Integrity Constraints (cont.)

Entity Integrity» The primary key attributes PK of each relation schema R in S cannot have null values in any tuple of r(R), i.e.,

t[PK] <> null for any tuple t in r(R)

Page 23: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 23Database

Relational Integrity Constraints (cont.)

Referential Integrity A constraint used to specify a relationship a

mong tuples in two relations: the referencing relation and the referenced relation

Tuples in the referencing relation R1 have attributes FK (called foreign key attributes) that reference the primary key attributes PK of the referenced relation R2

Page 24: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 24Database

Relational Integrity Constraints (cont.)

A tuple t1 in R1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK]

A referential integrity constraint can be displayed in a relational database schema as a directed arc from R1.FK to R2

A foreign key can refer to its own relation » e.g., the attribute SUPERSSN in EMPLOYEE ref

ers to the supervisor of an employee

Page 25: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 25Database

Relational Integrity Constraints (cont.)

Page 26: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 26Database

Relational Integrity Constraints (cont.)

Other types of constraints Semantic integrity constraints

» e.g., the salary of an employee should not exceed the salary of the employee’s supervisor

Transition constraints» Used to deal with state changes in the database» e.g., the salary of an employee can only increase

Page 27: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 27Database

Update Operations on Relations

Three basic update operations INSERT a tuple DELETE a tuple MODIFY a tuple

Integrity constraints should not be violated by the update operations

Page 28: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 28Database

Update Operations on Relations (cont.)

Insert can violate any of the four types of constraints» Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, null, ‘1960-04-0

5’, ‘6357 Windy Lane, Katy, TX’, F, 28000, null, 4> into EMPLOYEE

– violates the entity integrity constraint (null for the primary key SSN)

» Insert <‘Alicia’, ‘J’, ‘Zelaya’, ‘999887777’, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F, 28000, ‘987654321’, 4> into EMPLOYEE

– violates the key constraint because ‘999887777’ exists

Page 29: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 29Database

Update Operations on Relations (cont.)

The Delete operation can violate only referential integrity, if the tuple being deleted is referenced by the foreign keys from other tuples in the database» Delete the EMPLOYEE tuple with SSN = ‘99988

7777’ – is not acceptable, because tuples in WORKS_ON refe

r to this tuple

» Delete the EMPLOYEE tuple with SSN = ‘333445555’

– result in even worse referential integrity violations

Page 30: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 30Database

Update Operations on Relations (cont.)

Updating an attribute that is neither a primary key nor a foreign key usually causes no problems

Modifying a primary key value is similar to deleting one tuple and inserting another in its place

If a foreign key attribute is modified, the DBMS must make sure that the new value refers to an existing tuple in the referenced relation (or is null)

Page 31: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 31Database

Chapter Summary

Presented relational model concepts Definitions Characteristics of relations

Discussed relational model constraints and relational database schemas Domain constraints’ Key constraints Entity integrity Referential integrity

Described the relational update operations and dealing with constraint violations

Page 32: DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model

IM ISU 32Database

In-Class Exercise(Taken from Exercise 5.15)Consider the following relations for a database that keeps

track of student enrollment in courses and the books adopted for each course:

STUDENT(SSN, Name, Major, Bdate)COURSE(Course#, Cname, Dept)ENROLL(SSN, Course#, Quarter, Grade)BOOK_ADOPTION(Course#, Quarter, Book_ISBN)TEXT(Book_ISBN, Book_Title, Publisher, Author)Draw a relational schema diagram specifying the

foreign keys for this schema.