class5.PPT
class5.PPT
<number
Relational Query Languages
<number
Formal Relational Query
Languages
Two mathematical Query Languages form the
basis for “real” languages (e.g. SQL), and for
implementation:
• Relational Algebra: More operational, very
useful for representing execution plans.
• Relational Calculus: Lets users describe
what they want, rather than how to
compute it. (Non-operational, declarative.)
<number
Relational Algebra
• Basic operations:
⚬ Selection ( ) Selects a subset of rows from relation.
⚬ Projection ( ) Deletes unwanted columns from relation.
⚬ Cross-product ( ) Allows us to combine two relations.
⚬ Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
⚬ Union ( ) Tuples in reln. 1 and in reln. 2.
• Additional operations:
⚬ Intersection, join, division, renaming: Not essential, but (very!)
useful.
• Since each operation returns a relation, operations can be
composed! (Algebra is “closed”.)
<number
Projection
• Deletes attributes that are not in
projection list.
• Schema of result contains exactly
the fields in the projection list,
with the same names that they
had in the (only) input relation.
• Projection operator has to
eliminate duplicates! (Why??)
⚬ Note: real systems typically
don’t do duplicate elimination
unless the user explicitly asks
for it. (Why not?)
<number
Selection
<number
Union, Intersection, Set-
Difference
• All of these operations
take two input relations,
which must be union-
compatible:
⚬Same number of
fields.
⚬`Corresponding’ fields
have the same type.
• What is the schema of
result?
<number
Cross-Product
• Each row of S1 is paired with each row of R1.
• Result schema has one field per field of S1
and R1, with field names `inherited’ if
possible.
⚬Conflict: Both S1 and R1 have a field
called sid.
<number