0% found this document useful (0 votes)
16 views

Chapter 2

The chapter discusses the relational model of data including an overview of data models, the basics of the relational model, and relational algebra. It describes how the relational model uses relations that are composed of rows and columns. It also explains keys, dependencies, different types of keys including primary and foreign keys. Finally it covers the basic relational algebra operations including set operations, selection and projection, Cartesian products and joins.

Uploaded by

Hân Đinh Bảo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Chapter 2

The chapter discusses the relational model of data including an overview of data models, the basics of the relational model, and relational algebra. It describes how the relational model uses relations that are composed of rows and columns. It also explains keys, dependencies, different types of keys including primary and foreign keys. Finally it covers the basic relational algebra operations including set operations, selection and projection, Cartesian products and joins.

Uploaded by

Hân Đinh Bảo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Chapter 2

The Relational Model of Data

THE RELATIONAL MODEL OF DATA 1


Objectives
Understand what is the relational model and
database design basing relational model.
Conceptualize data using the relational model.
Understand what basic relational algebra
operators under set semantics.
Express queries using relational algebra.

THE RELATIONAL MODEL OF DATA 2


Contents

2.1 An Overview of Data Models


2.2 Basics of the Relational Model
2.3 An Algebraic Query Language

THE RELATIONAL MODEL OF DATA 3


2.1 An Overview of Data Models
 Data model: a collection of concepts for describing
data, including 3 parts:
 Structure of the data
 Ex: arrays or objects
 Operations on the data
 Queries and modification on data
 Constraints on the data
 Limitations on the data
 What is the difference between Data modeling and
Data Model?

THE RELATIONAL MODEL OF DATA 4


2.1 An Overview of Data Models
 The Evolution of Data Models

THE RELATIONAL MODEL OF DATA 5


2.2 Basics of the Relational Model

Relational model is known as a relation (sometimes


called a table) as a two-dimensional structure
composed of intersecting rows and columns
 Each row in a relation is called a tuple.
 Each column represents an attribute.

THE RELATIONAL MODEL OF DATA 6


2.2 Basics of the Relational Model

 A relation is made up from 2 parts:


 Schema: specifies name of relation, name of attributes
and domain/type of one’s.
 Ex: Student(StudentID: string, Name: string, Registered: int,
CounsellorNo: int, Region: int)
 Instance: a table with rows and columns
 Rows ~ cardinality; columns ~ degree/arity

THE RELATIONAL MODEL OF DATA 7


2.2 Basics of the Relational Model

THE RELATIONAL MODEL OF DATA 8


2.2 Basics of the Relational Model
Database schema: a set of schemas for the
relations of a database
An example of DB schema:

THE RELATIONAL MODEL OF DATA 9


2.2 Basics of the Relational Model
 Keys:
 Keys are important because they are used to ensure that each
row in a table is uniquely identifiable.
 They are also used to establish relationships
among tables and to ensure the integrity of the data.
 A key consists of one or more attributes that determine other
attributes.
Dependencies:
 The role of a key is based on the concept of determination
 Functional dependence
◦ determinant
◦ dependent

THE RELATIONAL MODEL OF DATA 10


2.2 Basics of the Relational Model
Type of Keys:
 Super key: An attribute or combination of attributes that uniquely
identifies each row in a table
 Candidate key: A candidate key is a minimal superkey
 Primary key: A candidate key selected to uniquely identify all other
attribute values in any given row; cannot contain null entries
 Foreign key An attribute or combination of attributes in one table
whose values must either match t
 A foreign key (fK) is an attribute or combination of attributes in one
table whose values must either match the primary key in another
table or be null
 A secondary key is defined as a key that is used strictly for data
retrieval purposes

THE RELATIONAL MODEL OF DATA 11


2.2 Basics of the Relational Model
Entity integrity
 Use PK
 The primary key has two requirements:
◦ (1) all of the values in the primary key must be unique and
◦ (2) no key attribute in the primary key can contain a null.
Referential integrity
 Use fk key
 Every foreign key entry must either be null or a valid value in
the primary key of the related table.

THE RELATIONAL MODEL OF DATA 12


2.3 An Algebraic Query Language

Relational Algebra
 An algebra consists of operators and atomic
operands
 Relational algebra is an example of an algebra,
its atomic operands are
 Variables that stand for relations
 Constants, which are finite relations
 Relational algebra is a set of operations on
relations
 Operations operate on one or more relations to
create new relation
THE RELATIONAL MODEL OF DATA 13
2.3 An Algebraic Query Language
Relational algebra fall into four classes
 Set operations – union(giao), intersection(hợp),
difference(hiệu or loại trừ)
 Selection and projection (chọn và chiếu)
 Cartesian product and joins
 Rename

THE RELATIONAL MODEL OF DATA 14


2.3 An Algebraic Query Language
 Set operations R and S must be ‘type
compatible’
Union  The same number of
R  S = { t | t  R  t  S} attributes
 The domain of
Intersection corresponding
R  S = { t | t  R  t  S} attributes must be
compatible
Difference
R \ S = { t | t  R  t  S}
Intersection can be expressed
in terms of set difference
R  S = R \ (R \ S)
THE RELATIONAL MODEL OF DATA 15
2.3 An Algebraic Query Language

THE RELATIONAL MODEL OF DATA 16


Set operations- Example

name address gender birthdate


Carrie Fisher 123 Maple St., Holywood F 9/9/99
Mark Hamill 456 Oak Rd., Brentwood M 8/8/88

Relation R

name address gender birthdate


Carrie Fisher 123 Maple St., Holywood F 9/9/99
Harrison Ford 789 Palm Dr., Beverly Hills M 8/8/88

Relation S

THE RELATIONAL MODEL OF DATA 17


Set operations- Example

RS name address gender birthdate


Carrie Fisher 123 Maple St., Holywood F 9/9/99
Mark Hamill 456 Oak Rd., Brentwood M 8/8/88
Harrison Ford 789 Palm Dr., Beverly Hills M 8/8/88

RS name address gender birthdate


Carrie Fisher 123 Maple St., Holywood F 9/9/99

R\S name address gender birthdate


Mark Hamill 456 Oak Rd., Brentwood M 8/8/88

THE RELATIONAL MODEL OF DATA 18


Selection and projection
 Selection
- R1 := σC (R2) with C illustrated conditions
 - ex:  <C1>( < C2> ( R)) =  <C2> ( < C1> ( R)) =  <C1> AND < C2>
Movies
title year length genre
Gone With the Wind 1939 231 Drama
Star Wars 1977 124 Scifi
Wayne’s World 1992 95 Comedy

σlength100(Movies)
title year length genre
Gone With the Wind 1939 231 Drama
Star Wars 1977 124 Scifi

THE RELATIONAL MODEL OF DATA 19


Selection and projection
 Projection S := πA1,A2,…,An (R)
 A1,A2,…,An are attributes of R
 S relation schema S(A1,A2,…,An)
Movies title year length genre
Star Wars 1977 124 Scifi
Galaxy Quest 1999 104 Comedy
Wayne’s World 1992 95 Comedy

title,year,length(Movies) genre(Movies)
title year length genre
Star Wars 1977 124 Scifi
Galaxy Quest 1999 104 Comedy
Wayne’s World 1992 95

THE RELATIONAL MODEL OF DATA 20


Cartesian product and joins

Cartesian product R3 := R1 Χ R2

Relation R Relation S Cartesian Product R X S


A B B C D A R.B S.B C D
1 2 2 5 6 1 2 2 5 6
3 4 4 7 8 1 2 4 7 8
9 10 11 1 2 9 10 11
3 4 2 5 6
3 4 4 7 8
3 4 9 10 11

THE RELATIONAL MODEL OF DATA 21


Cartesian product and joins
 theta joins R3 := R1 ⋈<join condition> R2

A B C B C D A U.B U.C V.B V.C D


1 2 3 2 3 4 1 2 3 2 3 4
6 7 8 2 3 5 1 2 3 2 3 5
9 7 8 7 8 10 1 2 3 7 8 10
6 7 8 7 8 10
Relation U Relation V
9 7 8 7 8 10

Figure 2.17: Result of U ⋈ A<D V

A U.B U.C V.B V.C D


1 2 3 7 8 10
Result of U ⋈ A<D AND U.BV.B V

THE RELATIONAL MODEL OF DATA 22


Cartesian product and joins
Natural join R3 := R1 ⋈ R2

Relation R Relation S Natural Join R ⋈ S


A B B C D A B C D
1 2 2 5 6 1 2 5 6
3 4 4 7 8 3 4 7 8
9 10 11

THE RELATIONAL MODEL OF DATA 23


Rename

The  operation gives a new schema to a relation


ρS(A1,…,An)(R) makes S be a relation with attributes A1,
…,An and the same tuples as R
Simplified notation: S:=R (A1,A2,…,An)
Relation R Relation S RX S(X,C,D) (S)
A B B C D A B X C D
1 2 2 5 6 1 2 2 5 6
3 4 4 7 8 1 2 4 7 8
9 10 11 1 2 9 10 11
3 4 2 5 6
3 4 4 7 8
3 4 9 10 11

THE RELATIONAL MODEL OF DATA 24


Relational Expression
 How we need relational expression
 Relational algebra allows us to form expressions
 Relational expression is constructed by applying
operations to the result of other operations
 Expressions can be presented as expression
tree

THE RELATIONAL MODEL OF DATA 25


The role of relational algebra in a DBMS

THE RELATIONAL MODEL OF DATA 26


Relational Expression
Example: What are the titles and years of
movies made by Fox that are at least 100
minutes long?
 (1) Select those Movies tuples that have length
 100
 (2) Select those Movies tuples that have
studioName=‘Fox’
 (3) Compute the intersection of (1) and (2)
 (4) Project the relation from (3) onto attributes
title and year

THE RELATIONAL MODEL OF DATA 27


Relational Expression

title,year

length>=100 studioName=‘Fox’

Movies Movies

Figure 2.18: Expression tree for a relational algebra expression

title,year(length100 (Movies)  studioName=‘Fox’ (Movies))

title,year(length100 AND studioName=‘Fox’ (Movies))

THE RELATIONAL MODEL OF DATA 28


More exercises

Example
Section relation

29
Union Operation

Example: to find all courses taught in the Fall 2017


semester, or in the Spring 2018 semester, or in both
course_id ( semester=“Fall” Λ year=2017 (section)) 

course_id ( semester=“Spring” Λ year=2018 (section))

The result of this expression, shown in the next


slide

30
Union Operation

section relation
result

31
Set-Intersection Operation

Example: Find the set of all courses taught


in both the Fall 2017 and the Spring 2018
semesters.
course_id ( semester=“Fall” Λ year=2017 (section)) 
course_id ( semester=“Spring” Λ year=2018 (section))
Result:

32
Set Difference Operation

Example: to find all courses taught in the


Fall 2017 semester, but not in the Spring
2018 semester
course_id ( semester=“Fall” Λ year=2017 (section)) −
course_id ( semester=“Spring” Λ year=2018 (section))
Result:

33
Equivalent Queries
There is more than one way to write a query in relational
algebra.
Example: Find information about courses taught by instructors
in the Physics department with salary greater than 90,000
Query 1
 dept_name=“Physics”  salary > 90,000 (instructor)
Query 2
 dept_name=“Physics” ( salary > 90.000 (instructor))

The two queries are not identical; they are, however, equivalent
-- they give the same result on any database.

34
Equivalent Queries

35
Exercises
Example: Customer
Ex1:

Query 1: List customers whose cred_lim is greater than £500.


Query 2: List customers whose cred_lim is greater than £500
and lives in London.
36
Exercises sid bid day
Reserves 22 101 10/10/11
Ex2 58 103 11/12/11
sid sname rating age
bid bname color
22 Jesly 7 45.0 101 Interlake Blue
31 Mishail 8 55.5 102 Interlake Red
58 Raj 10 35.0 103 Clipper Green
sailors 104 Marine Red
Boats
1.Find names of sailors who’ve reserved boat #103
2.Find names of sailors who’ve reserved a red boat
3.Find sailors who’ve reserved a red or a green boat
4.Find sailors who’ve reserved a red and a green boat
5. Find the names of sailors who’ve reserved all boats
37

You might also like