The Relational Model Chapter 3 and 6.1

Acknowledgements
The Relational Model
Chapter 3 and 6.1-5
• These slides were written by Richard T. Snodgrass
(University of Arizona) and Christian S. Jensen
(Aalborg University) and Curtis Dyreson (Utah State
University).
• Kristian Torp (Aalborg University) converted the slides
from island presents to Powerpoint.
CS 5800
Utah State University
Introduction to the Relational Model
Overview
R-2
Sets
• A set is an unordered collection of distinct objects.
• The Relational Model (RM)
• Integrity Constraints
• Set operations include
• Relational Algebra
Introduction to the Relational Model
Examples
{3, 4, a} is a set
{3, 4, a} is the same set as {a, 4, 3}
{4, 4} is not a set
Relations
Properties of relations
R-3
Intersection, e.g., {3, 4, a} I {b, 4} = {4}
Union, e.g., {3, 4, a} U {b, 4} = {3, 4, a, b}
Difference, e.g., {3, 4, a} - {b, 4} = {3, a}
Introduction to the Relational Model
Domains and Attributes
Relations
Definition: A domain is a set of values of some "type"
Definition: Given sets A1, A2, ... , An a relation r is a
subset of their Cartesian product.
r ⊆ A1 × A2 × ... × An
• r is a set of n-tuples (a1, a2, ... , an) where ai ∈ Ai
Positive integers = {1, 2, 3, 4, …}
Alphanumeric characters = {‘a’, ‘b’, …, ‘Z’, ‘0’, …, ‘9’}
R-4
• A common database restriction: A domain is atomic.
Definition: R(A1, A2, ..., An) is the schema of relation r.
The following programming types are atomic domains.
integer, char, float, varchar
Structs/records are a composite domain.
A1, A2, ... , An are domains; their names are attributes
R is the name of the relation
struct { a: int, b: char } record_definition
• Notation: r(R) is a relation on the relation schema R.
Definition: An attribute is the name of a domain.
Introduction to the Relational Model
R-5
Introduction to the Relational Model
R-6
Example, The < Relation
Relations as Tables
• Let O be the domain {1, 3} and E be {0, 2}.
• Relations can be depicted as tables (approx.)
• ×(O,E) where O = {1,3} and E = {0,2}
O × E = {(1,0), (1,2), (3,0), (3,2)}
×
1
0
3
2
1
0
3
• <(O,E) is the schema, O, E are attributes, {1,3} and
0
3
2
Introduction to the Relational Model
E
1
0
1
2
3
0
3
2
O
1
E
2
• <(O,E)
{0,2} are domains
O < E = {(1,2)} ⊆ O × E
1
2
O
R-7
1
0
3
2
<
Introduction to the Relational Model
R-8
Tuples
Characteristics of Relations
Definition: An element t of a relation r is called a tuple.
• Tuples in a relation are unordered.
• We refer to component values of a tuple t by t[Ai] = vi
• Example: The following two relations have the same
information content.
(the value of attribute Ai for tuple t).
Alternatively, use ‘dot’ notation, e.g., t.Ai is the ith attribute
of tuple t
Name Age
• t[Ai, ... , Ak] refers to the subtuple of t containing the
•
Pat
values of attributes Ai, ... , Ak respectively.
Table metaphor
Tuple is a row
Attribute is a column
Name Age
1
Sue
Pam 4
Sue
Fred 2
3
Pam 4
Introduction to the Relational Model
R-9
3
Fred 2
Pat
1
Introduction to the Relational Model
R-10
Characteristics of Relations (cont.)
Characteristics of Relations (cont.)
• Attributes in a tuple/relation are ordered.
• Values in a tuple are atomic (indivisible).
• A value cannot be a structure, record, or relation
But, we can change the order at will (to be shown later).
Example: The following relations do not have the same
information.
Example: All atomic values (string or integer).
Name Age
Pat
Name Age
Pat
1
1
2 Fred
Sue
3
3
1
Fred 2
Pat
Fred 2
Pam 4
Introduction to the Relational Model
Age Name
Example: The following is not a relation, Name is not atomic.
Sue
Name
4 Pam
R-11
Introduction to the Relational Model
Age
First
Last
Joe
Doe
First
Last
Sue
Doe
1
2
R-12
Degree and Cardinality
Ord
102
103
104
105
107
108
110
111
112
CID
42
39
24
42
34
46
24
21
29
Date
860207
860211
860228
860303
860308
860308
860312
860318
860320
Overview
• The Relational Model (RM)
Salesperson
Johnson
Strong
Boggs
Strong
West
West
Boggs
Strong
Clark
• Integrity Constraints
Tuples (or records, rows)
[Cardinality is # of them]
Overview
Key constraints
◆
◆
Super, candidate, and primary keys
Foreign keys and referential integrity
Other constraints
degree/arity
• Mapping from the ER Model to the Relational Model
Introduction to the Relational Model
R-13
Introduction to the Relational Model
R-14
Integrity Constraints
Important Aspects of Integrity Constraints
• An integrity constraint is a predicate that is required to be true
• Description
•
of every possible instance of the database schema.
Purpose: To keep the database is a “good” state.
Are specified at the schema level
◆
A good state is one that is consistent with the modeled reality.
◆
• How: Check to see if an update will move to a “bad” state.
A constraint must be true for all possible instances of a schema.
Critical notion: possible instances
Some can be specified (conceptually) with relational algebra.
In commercial databases, specified with SQL (typically).
In ER Design tools, specified as properties of entity/relationship types
• Enforcement
state
X
update
update
state
Evaluate on update (insert/delete)
Cost of enforcement
◆
◆
state
Only allow specification of ICs that can be tested efficiently.
How many to evaluate?
• Remedy for violation
Good states
See next slide
All states
Introduction to the Relational Model
R-15
Introduction to the Relational Model
Enforcement of Remedy
Key Constraints
• When an integrity violation occurs either
Definition: A superkey of R is a set of attributes K of R
such that no two tuples in any valid relation instance
r(R) will have the same value for K.
Cancel the operation that causes the violation.
Perform the operation but inform the user of the violation.
Trigger additional updates so the violation is corrected.
Execute a user-specified error-correction routine.
For any distinct tuples t1 and t2 in r(R), t1[K] ≠ t2[K]
Definition: A candidate key of R is a minimal superkey;
that is, a superkey K such that removal of any attribute
from K results in a set of attributes that is not a
superkey.
Definition: A primary key is a specified candidate key.
Definition: A foreign key is a set of attributes that is a
key in some relation (in the schema of the database).
• Specified for each individual constraint
Introduction to the Relational Model
R-16
R-17
Introduction to the Relational Model
R-18
Example
Keys, Fact and Fiction
• Consider two relation schemas (with no keys specified).
• A key is often more than a single attribute
Customer(ID, Name)
Reserved(CustomerID, FilmID, ResDate)
• Assumption: No two customers have the same ID
Superkeys of Customer
• An attribute in a key can be of any type
{ID}
{ID, Name}
Candidate key of Customer (and also the primary key, since only one)
{ID}
• Assumption: Customer can rent many films over several days.
Some DB products restrict keys to integer or auto-number types
Easy for the software to check and maintain
Detracts from semantics
• Choosing a key is difficult
The superkey of Reserved
Time-invariant keys - schema changes over time
Converting after a “wrong choice” can be costly
{CustomerID, FilmID, ResDate}
Some DB products impose a single-attribute key restriction
The belief is that smaller keys are more efficient
Detracts from semantics
◆
One candidate key (see above), primary key (see above)
• CustomerID of Reserved is a foreign key referencing the
◆
primary key, ID, of Customer.
Introduction to the Relational Model
R-19
Entity Integrity
Assume SSN is a good key, new law makes using SSN illegal.
Keys are “shared” as foreign keys in other relations
Introduction to the Relational Model
R-20
Domain Constraints
• Two roles similar to programming languages
• The primary key attributes Pk = {k1, …, km} of each
1.
relation schema R in S cannot have null values in any
tuple of r(R).
2.
t[Pk] ≠ Null for any tuple t ∈ r(R)
Strong typing or type-by-name in programming languages.
Most DBMSs have only a restricted selection of simple,
predefined types available.
Each domain creates a separate type
• Consider these sample relation schemas.
Film (ID, Title)
Customer (ID, Name)
• This is because primary key values are used to identify
individual tuples.
It is not meaningful to compare Film and Customer IDs.
◆
◆
◆
Introduction to the Relational Model
R-21
Create FilmID domain of type integer
Create CustomerID domain of type integer
DBMS does not allow IDs of domain FilmIDs to be compared with
IDs of domain CustomerIDs.
Introduction to the Relational Model
Referential Integrity
Referential Integrity, Definition
• Referential integrity is the "glue" that keeps the
• Definition: Let r1 and r2 be instances of schemas R1
database together, ensuring the consistency of foreign
keys.
A foreign key is a key in some relation
A foreign key points to or references some relation
Referential integrity says that the information that is pointed
to must exist.
R-22
and R2, let K be the primary key for R1, and let α be a
subset of R2 that is compatible with K. Then, α is a
foreign key for K if every value for α in a tuple in r2
also appears as a value of K in a tuple in r1 .
• Constraints of this type are termed referential integrity
• Example:
constraints or subset constraints.
If a CustomerID, e.g., 123456, is associated with a FilmID,
e.g., 100001, in a tuple in the table Reserved, a customer with
CustomerID 123456 and a film with FilmID 100001 must
exist in the Customer and Film relations, respectively.
Introduction to the Relational Model
R-23
Introduction to the Relational Model
R-24
Enforcement of Referential Integrity
Assertions
• Consider the constraint that every foreign key value in
• Assertions are more general integrity constraints,
•
r2 is in the referenced relation r1 .
Insertion into r2:
•
Check that a corresponding tuple exists in r1 .
expressed directly as predicates which must always be
satisfied.
Think of these as sanity checks.
• Deletion from r1:
• Examples
Check that no matching tuple existed in r2.
• Updates of tuples in r2 are treated as insertions above.
• Updates of tuples in r1 are treated as deletions above.
• Note that cascading deletes are possible.
Introduction to the Relational Model
R-25
No video rents for less than 0$ (equivalently, the value of
RentalPrice must be no less than 0).
The spoken and subtitled languages for a film must be
different.
Introduction to the Relational Model
R-26
Triggers
Relational Algebra (RA)
• Triggers are executed automatically as side effects of
• Operators
•
database modifications.
To specify a trigger,
specify the condition for executing the trigger, and
specify the action to be taken when the trigger executes.
• Example queries
• Limitations
• Triggers are also called ECA rules, (Event-ConditionAction).
Complete set – projection, selection, Cartesian product,
difference, union
Convenient – intersection, theta join, equijoin, natural join,
semijoin, division
Events may be, e.g., on insertion or on update.
Conditions may (in principle) be arbitrary predicates.
Actions may (in principle) be arbitrary updates.
• Triggers are more procedural than assertions.
Introduction to the Relational Model
R-27
Relational Algebra
Introduction to the Relational Model
R-28
RA Operations Produce Relations
• Five primary operators (complete)
Selection: σ
Projection: π
Union: ∪
Difference: –
Cartesian Product: ×
Name Age
joe
4
sue
10
sam
9
operation1
Name Age
sue
10
sam
9
operation2
Name
sue
sam
• Convenient derived operators (closed)
Intersection: ∩
Theta join θ, equijoin = , and natural join Semijoins:  and 
Relational division: ÷
Introduction to the Relational Model
Notation:
R-29
operation2 ( operation1 (R) )
Introduction to the Relational Model
R-30
Selection, Informal Description
Selection, Formal Description
• Choose a row or rows, choice is based on a condition
•
•
•
or predicate
Result has same schema.
Remember: rows are unordered so cannot specify row
number, e.g., 2nd row or 4th row.
Example
Friends Name Age
joe
4
sue
10
sam
9
σP(r) = {t | t ∈ r ∧ P(t) }
• P is a formula in propositional calculus, dealing with terms of
the form:
Result
Name Age
sue
10
sam
9
Select rows from Friends such that Age > 8.
Introduction to the Relational Model
R-31
Selection Example
Introduction to the Relational Model
R-32
Projection, Informal Description
A
1
3
2
r:
attribute (or constant) = attribute (or constant)
attribute ≠ attribute
attribute < attribute
attribute ≤ attribute
attribute ≥ attribute
attribute > attribute
term ∧ term (Note: ∧ is AND)
term ∨ term (Note: ∨ is OR)
¬(term)
B
y
x
y
•
•
•
•
C
ω
γ
β
σB = y ∧ A > 1(r):
A
2
B
y
Project column(s), choice is based on column name
Result usually has different schema, fewer columns.
Remember: column are named, so can use name
Example
Friends Name Age
joe
4
sue
10
sam
9
C
β
Result Name
joe
sue
sam
Project the Name column from Friends.
Introduction to the Relational Model
R-33
Introduction to the Relational Model
R-34
Projection, Formal Description
Switching Columns
πX(r) = {t[X] | t ∈ r }
• Assume r is the following
• Let X = {Ai1, Ai2 , ... ,
}
• The result is a relation of n columns obtained by
•
keeping the columns that are specified.
Example
A
1
3
2
r:
B
y
x
y
Name Age
Pat
1
Fred 2
Sue
3
Pam 4
C
ω
γ
β
• To switch columns use projection
Age Name
πA,C (r):
Introduction to the Relational Model
A
1
3
2
C
ω
γ
β
πB (r):
πAge,Name (r) =
B
y
x
1
Pat
2 Fred
3
Sue
4 Pam
R-35
Introduction to the Relational Model
R-36
Video Store Schema (subset)
Video Store Queries
• Customer (CustomerID, Name, Street, City, State)
• List information for films with a rental price over $4.
• Film (FilmID, Title, RentalPrice, Kind)
σRentalPrice > 4(Film)
• Reserved (CustomerID, FilmID, ResDate)
• List the titles of films with a rental price over $4.
πTitle (σRentalPrice > 4(Film))
Introduction to the Relational Model
R-37
Union, Informal Description
Introduction to the Relational Model
R-38
Union, Formal Description
r ∪ s = {t | t ∈ r ∨ t ∈ s}
• Put all tuples in two relations into one, new relation
• r and s must be union-compatible:
• Remember: relation has unique elements, must
eliminate duplicates
Friends
Name Age
joe
4
sue
10
sam
9
r and s have the same arity.
The attributes of r and s are same over domains.
• Example
Result
Others Name Age
fred
8
sue
10
A
1
3
2
r:
Name Age
joe
4
sue
10
sam
9
fred
8
B
y
x
y
r∪s:
Perform Union of Friends and Others
Introduction to the Relational Model
R-39
Difference, Informal Description
Friends
Name Age
joe
4
sue
10
sam
9
B
y
x
y
w
B
w
x
C
ζ
γ
C
ω
γ
β
ζ
Introduction to the Relational Model
R-40
r – s = {t | t ∈ r ∧ ¬(t ∈ s)}
Example:
Result
Name Age
joe
4
sam
9
r:
Perform Friends minus Others.
Introduction to the Relational Model
A
1
3
2
4
A
4
3
s:
Difference, Formal Description
• Remove same tuples from a relation
• Schemas must be union compatible.
• Example
Others Name Age
fred
8
sue
10
C
ω
γ
β
R-41
A
1
3
2
B
y
x
y
C
ω
γ
β
r–s:
A
1
2
Introduction to the Relational Model
s:
B
y
y
A
4
3
B
w
x
C
ζ
γ
C
ω
β
R-42
Video Store Queries, cont.
Cartesian Product, Informal Description
• List outrageously priced films (over $4 or under $1).
• All possible combinations of tuples from two relations
• Schema of result is combined schema of both operands
Example:
πTitle (σRentalPrice > 4(Film) ∪ σRentalPrice < 1(Film))
Friends
Result
Name Age
joe
4
sue
10
sam
9
• List the IDs of the expensive films that have not been
reserved.
Name Age Alias Size
joe
4
bob
8
joe
4
grr
10
sue
10 bob
8
sue
10
grr
10
sam
9
bob
8
sam
9
grr
10
Alias Size
bob
8
grr
10
πFilmId (σRentalPrice > 4(Film)) – πFilmId(Reserved)
Others
Perform Cartesian Product of Friends and Others.
Introduction to the Relational Model
R-43
Cartesian Product
Introduction to the Relational Model
Overview
r × s = {t q | t ∈ r ∧ q ∈ s}
Relational Algebra (RA)
Operators
◆
• The schema of the resulting relation is R S
◆
r:
A
1
3
2
B
y
x
y
C
ω
γ
β
s:
D
“Tom”
“Eric”
A
1
2
r×s:
A
1
3
2
1
3
2
B
y
x
y
y
x
y
C
ω
γ
β
ω
γ
β
D
“Tom”
“Tom”
“Tom”
“Eric”
“Eric”
“Eric”
A
1
1
1
2
2
2
Introduction to the Relational Model
R-46
r ∩ s = {t | t ∈ r ∧ t ∈ s} = r – (r – s)
Example:
Result
Name Age
sue
10
r:
Name Age
fred
8
sue
10
A
1
3
2
B
y
x
y
r∩s:
Perform Intersection of Friends and Others
Introduction to the Relational Model
Introduction to the Relational Model
Intersection, Formal Description
• Keep same tuples from two relations
• Schema must be the same for all three
• Example
Others
Example queries
Limitations
Extended Relational Algebra Operators
• Domain Relational Calculus (DRC)
R-45
Name Age
joe
4
sue
10
sam
9
Complete set – projection, selection, Cartesian product, difference,
union
Convenient – intersection, theta join, equijoin, natural join, semijoin,
division
• Tuple Relational Calculus (TRC)
Intersection, Informal Description
Friends
R-44
R-47
Introduction to the Relational Model
C
ω
γ
β
A
3
s:
B
x
A
4
3
B
w
x
C
ζ
γ
C
γ
R-48
Video Store Queries, cont.
Joins
• List the ID numbers of the films that are expensive and
• Joins are Cartesian products coupled with selections
and projections.
have been reserved.
• Theta join
πFilmId (σRentalPrice > 4(Film)) ∩ πFilmId(Reserved)
r θ s = σθ (r × s)
Example: r A < E s = σA < E (r × s)
• Equijoin
A theta join in which θ is an equality predicate
Example: r A = E s = σA = E (r × s)
• Natural join 
• Semijoins
Introduction to the Relational Model
R-49
Theta Join, Informal Description
Left semijoin 
Right semijoin 
Introduction to the Relational Model
Theta Join, Formal Description
R θ s = σθ (r × s)
• Try all possible combinations of tuples from two
•
•
relations, keep only those that match a condition
Schema of result is combined schema of both operands
Example
Friends Name Age
joe
4
sue
10
sam
9
Result
Name Age Alias Size
joe
4
bob
8
joe
4
grr
10
sam
9
grr
10
Alias Size
bob
8
grr
10
• Example
r:
A
1
0
2
B
y
x
y
r A<E s :
• Equijoin
Others
Perform theta join between Friends and Others on Age < Size.
Introduction to the Relational Model
R-50
R-51
C
ω
γ
β
A
1
0
0
2
s:
B
y
x
x
y
C
ω
γ
γ
β
D
“Tom”
“Eric”
D
“Tom”
“Tom”
“Eric”
“Tom”
E
3
1
E
3
3
1
3
A theta join in which θ is an equality predicate
Example: r A=E s
Introduction to the Relational Model
R-52
Video Store Queries, cont.
Natural Join, Informal Description
• List the titles of all reserved films.
• Do equi-join, then eliminate duplicate columns
• Schema of result is combined schema of both operands
• Example
πTitle (σFilm.FilmID = Reserved.FilmID(Film × Reserved))
πTitle (Film Film.FilmID = Reserved.FilmID Reserved)
Friends
• List the customers who have reserved a film.
πName (σCustomer.CustomerID = Reserved.CustomerID
(Customer × Reserved))
Alias Age
bob
8
grr
10
πName (Customer Film.FilmID = Reserved.FilmID Reserved)
Introduction to the Relational Model
Name Age
joe
4
sue
10
sam
9
Result
Name Age Alias Age
sue
10
grr
10
Others
Perform natural join of Friends and Others.
R-53
Introduction to the Relational Model
R-54
Natural Join, Formal Description
Video Store Queries, cont.
• List the customers who have reserved expensive films.
r  s= πR∪S (r = s)
πName (σRentalPrice > 4 (Customer  Reserved  Film))
• Schema of result is R∪S
• Let t be a tuple in the result.
πName (Customer  σRentalPrice > 4 (Reserved  Film))
t[R] has the same value as a tuple tR ∈ r.
t[S] has the same value as a tuple ts ∈ s.
πName (Customer  Reserved  σRentalPrice > 4 (Film))
• Example
A
1
3
2
r:
B
y
x
y
C
ω
γ
β
A
1
3
2
rs:
s:
D
“Tom”
“Eric”
B
y
x
• List the streets of customers who have reserved foreign
films.
B
y
x
y
C
ω
γ
β
D
“Tom”
“Eric”
“Tom”
Introduction to the Relational Model
πStreet (Customer  Reserved  σKind = “F” (Film))
R-55
Semijoins
• Assume that relation r is very large, and resides in a
database in Los Angeles.
• The result has the same schema as the left-hand
• Relation s is small, and resides in a database in New
argument, r.
York City.
• Example
A
1
3
2
B
y
x
y
C
ω
γ
β
s:
D
“Tom”
• Application desires r  s, in New York City.
• Sending all of r from Los Angeles to New York City
B
y
•
A
1
2
B
y
y
C
ω
β
• Right semijoin: r  s = πS (r  s)
Introduction to the Relational Model
R-57
Relational Division
C
ω
γ
β
ω
A
1
R-58
respectively, where
r÷s:
Introduction to the Relational Model
Introduction to the Relational Model
• Let r and s be relations on schemes R and S
• Example
B
y
x
y
z
New York City sends πR∩S (s) to Los Angeles.
Los Angeles computes r s, and sends the result to New
York City. This table is significantly smaller than r.
New York City computes (r  s)  s = r  s
Relational Division, cont.
r ÷ s = { t | ∀u ∈s (t u ∈ r)}
r:
would be very expensive.
Alternative processing strategy:
rs:
A
1
3
2
1
R-56
Semijoins in Distributed Query Proc.
r  s = πR (r  s)
r:
Introduction to the Relational Model
s:
B
y
z
R = (A1, A2, ..., An, B1, B2, ..., Bn,)
S = (B1, B2, ..., Bn,)
• The result of r divided by s is a relation on scheme
R ÷ S = (A1, A2, ..., An)
• r ÷ s = πR – S (r) – πR – S ((πR – S (r) × s) – r)
C
ω
R-59
Introduction to the Relational Model
R-60
Video Store Queries, cont.
Video Store Queries, cont.
• List the customers who have reserved all the foreign
• Find the film(s) with the highest rental price.
• We need a renaming operator: ρName
films.
Identify the foreign films.
πFilmID (σKind = “F” (Film))
• Alternative formulation: Find the film(s) with a rental
Identify those customers who have reserved all foreign films.
πCustomerID,FilmID (Reserved) ÷πFilmID (σKind = “F” (Film))
Now figure out the names of those customers.
πName (Customer  (πCustomerID,FilmID (Reserved)
÷ πFilmID (σKind = “F” (Film))))
Introduction to the Relational Model
R-61
price for which no other rental price is higher.
πTitle(Film) –
πF2.Title (σFilm.RentalPrice > F2.RentalPrice (Film×ρF2(Film)))
Introduction to the Relational Model
Relational Completeness
Limitations of the Algebra
• All the operators can be expressed in terms of the five
• Can't do arithmetic.
basic operators: σ, π, −, ×, ∪
R-62
Find the rental price assuming a 10% increase.
• Can't do aggregates.
• This set is called a complete set of relational algebraic
operators.
• Any query language that is at least as powerful as these
operators is termed (query) relationally complete.
How many films has each customer reserved?
• Can't handle “missing” data.
Make a list of the films, along with who reserved it, if
applicable.
• Can't perform transitive closure.
For a partof(Part, ConstituentPart) relation, find all parts in
the car door.
• Can't sort, or print in various formats.
Print a reserved summary, sorted by customer name.
• Can't modify the database.
Introduction to the Relational Model
R-63
Extending the Projection Operator
Increase all $3.25 rentals to $3.50.
Introduction to the Relational Model
R-64
Aggregates
• The generalized projection operator allows expressions
in addition to column names in the subscript.
• Find the rental price, assuming a 10% increase.
πTitle, RentalPrice*1.1(Film)
(r) =
t a1 ... ak | t ∈ r ∧
a1 = agg1 ({u | u ∈ r ∧ u[G] = t[G]})
....
ak = aggk ({u | u ∈ r ∧ u[G] = t[G]})
G agg1, ..., aggk
• Appends new attributes.
• Each aggregate function aggi decides which attribute
to aggregate over.
• The grouping attributes G are optional.
Introduction to the Relational Model
R-65
Introduction to the Relational Model
R-66
Aggregates, cont.
Outer Joins
• How many reservations are there?
• In a regular equijoin or natural join, tuples in r or s that do not
πcount(
•
count (Reserved))
•
r L s = r n s ∪ ((r – (r l s)) × (null, ..., null))
• Right outer join:
r R s = r n s ∪ ((null, ..., null) × (s – (s l r)))
• Full outer join:
r N s = r n s ∪ ((r – (r l s)) × (null, ..., null))
∪ ((null, ..., null) × (s - (s l r)))
• How many reservations are there at each rental price?
πRentalPrice, count(RentalPrice
count (Reserved
n Film))
Introduction to the Relational Model
R-67
Outer Join Example
r:
A
1
3
2
r N s:
Introduction to the Relational Model
B
y
x
z
A
1
3
2
Null
C
ω
γ
β
B
y
x
z
w
s:
C
ω
γ
β
Null
D
“Tom”
“Eric”
“Melanie”
having matching tuples in the other relation do not appear in the
result.
The outer joins retain these tuples, and place nulls in the missing
attributes.
Left outer join:
B
y
x
w
D
“Tom”
“Eric”
Null
“Melanie”
R-69
Introduction to the Relational Model
R-68