03-Relational Model
03-Relational Model
Outline
.2
Example of a Instructor Relation
attributes
(or columns)
tuples
(or rows)
.3
Relation Schema and Instance
Example:
instructor = (ID, name, dept_name, salary)
Formally, given sets D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn
Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai Di
.4
Attributes
The set of allowed values for each attribute is called the domain of the
attribute
Attribute values are (normally) required to be atomic; that is, indivisible
The special value null is a member of every domain. Indicated that the
value is “unknown”
The null value causes complications in the definition of many operations
.5
Relations are Unordered
.6
Database Schema
.7
Keys
Let K R
K is a superkey of R if values for K are sufficient to identify a unique tuple of
each possible relation r(R)
Example: {ID} and {ID,name} are both superkeys of instructor.
Superkey K is a candidate key if K is minimal
Example: {ID} is a candidate key for Instructor
One of the candidate keys is selected to be the primary key.
which one?
Foreign key constraint: Value in one relation must appear in another
Referencing relation
Referenced relation
Example – dept_name in instructor is a foreign key from instructor
referencing department
.8
Schema Diagram for University Database
.9
Relational Query Languages
Procedural versus non-procedural, or declarative
“Pure” languages:
Relational algebra
Tuple relational calculus
Domain relational calculus
The above 3 pure languages are equivalent in computing power
We will concentrate in this chapter on relational algebra
Not Turing-machine equivalent
Consists of 6 basic operations
.10
Relational Algebra
A procedural language consisting of a set of operations that take one or
two relations as input and produce a new relation as their result.
Six basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
.11
Select Operation
dept_name=“Physics” (instructor)
Result
.12
Select Operation – selection of rows (tuples)
Relation r
.13
Select Operation (Cont.)
.14
Project Operation
A unary operation that returns its argument relation, with certain attributes
left out.
Notation:
.15
Project Operation – selection of columns (Attributes)
Relation r:
A,C (r)
.16
Project Operation Example
Example: eliminate the dept_name attribute of instructor
Query:
ID, name, salary (instructor)
Result:
.17
Composition of Relational Operations
.18
Cartesian-Product Operation
The Cartesian-product operation (denoted by X) allows us to combine
information from any two relations.
Example: the Cartesian product of the relations instructor and teaches is
written as:
instructor X teaches
We construct a tuple of the result out of each possible pair of tuples: one
from the instructor relation and one from the teaches relation (see next
slide)
Since the instructor ID appears in both relations we distinguish between
these attribute by attaching to the attribute the name of the relation from
which the attribute originally came.
instructor.ID
teaches.ID
.19
The instructor X teaches table
.20
joining two relations -- Cartesian-product
Relations r, s:
r x s:
.21
Cartesian-product – naming issue
Relations r, s: B
r x s: r.B s.B
.22
Composition of Operations
Can build expressions using multiple operations
Example: A=C (r x s)
rxs
A=C (r x s)
.23
Join Operation
The Cartesian-Product
instructor X teaches
associates every tuple of instructor with every tuple of teaches.
Most of the resulting rows have information about instructors who did
NOT teach a particular course.
To get only those tuples of “instructor X teaches “ that pertain to
instructors and the courses that they taught, we write:
instructor.id = teaches.id (instructor x teaches ))
.24
Join Operation (Cont.)
The table corresponding to:
instructor.id = teaches.id (instructor x teaches))
.25
The instructor X teaches table
.26
Join Operation (Cont.)
Thus
instructor.id = teaches.id (instructor x teaches ))
.27
Natural Join Example
Relations r, s:
Natural Join
r s
.28
Union Operation
.29
Union Operation (Cont.)
Result of:
course_id ( semester=“Fall” Λ year=2017 (section))
course_id ( semester=“Spring” Λ year=2018 (section))
.30
Union of two relations
Relations r, s:
r s:
.31
Set-Intersection Operation
The set-intersection operation allows us to find tuples that are in both
the input relations.
Notation: r s
Assume:
r, s have the same arity
attributes of r and s are compatible
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 intersection of two relations
Relation r, s:
rs
Note: r s = r – (r – s)
.33
Set Difference Operation
The set-difference operation allows us to find tuples that are in one relation
but are not in another.
Notation r – s
Set differences must be taken between compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible
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))
.34
Set difference of two relations
Relations r, s:
r – s:
.35
The Assignment Operation
It is convenient at times to write a relational-algebra expression by
assigning parts of it to temporary relation variables.
The assignment operation is denoted by and works like assignment in
a programming language.
Example: Find all instructor in the “Physics” and Music department.
.36
The Rename Operation
The results of relational-algebra expressions do not have a name that we
can use to refer to them. The rename operator, , is provided for that
purpose
The expression:
x (E)
returns the result of expression E under the name x
Another form of the rename operation:
x(A1,A2, .. An) (E)
.37
Renaming a Table
Allows us to refer to a relation, (say E) by more than one name.
x (E)
Relations r
.38
Equivalent Queries
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.
.39
Equivalent Queries
Query 2
(dept_name=“Physics” (instructor)) instructor.ID = teaches.ID teaches
The two queries are not identical; they are, however, equivalent -- they
give the same result on any database.
.40