CS121Lec02 PDF
CS121Lec02 PDF
CS121Lec02 PDF
¨ Important note:
¤ Result relation may have fewer rows than input relation!
¤ Why?
n Relations are sets of tuples, not multisets
Project Example
9
¨ Result only has three tuples, even though input has five
¨ Result schema is just (branch_name)
Composing Operations
10
¨ Written as: r ∪ s
¨ Result contains all tuples from r and s
¤ Each tuple is unique, even if it’s in both r and s
¨ Constraints on schemas for r and s ?
¨ r and s must have compatible schemas:
¤ r and s must have same arity
n (same number of attributes)
¤ For each attribute i in r and s, r[i] must have the same
domain as s[i]
¤ (Our examples also generally have same attribute names,
but not required! Arity and domains are what matter.)
Set-Union Example
12
¨ Written as: r – s
¨ Result contains tuples that are only in r, but not in s
¤ Tuples in both r and s are excluded
¤ Tuples only in s are also excluded
¨ Written as: r × s
¤ Read as “r cross s”
¨ No constraints on schemas of r and s
¨ Schema of result is concatenation of schemas for r and s
¨ If r and s have overlapping attribute names:
¤ All overlapping attributes are included; none are eliminated
¤ Distinguish overlapping attribute names by prepending the source
relation’s name
¨ Example:
¤ Input relations: r(a, b) and s(b, c)
¤ Schema of r × s is (a, r.b, s.b, c)
Cartesian Product Operation (2)
19
¨ Result of r × s
¤ Contains every tuple in r, combined with every tuple in s
¤ If r contains Nr tuples, and s contains Ns tuples, result
contains Nr × Ns tuples
¨ Allows two relations to be compared and/or
combined
¤ If we want to correlate tuples in relation r with tuples in
relation s…
¤ Compute r × s, then select out desired results with an
appropriate predicate
Cartesian Product Example
20
¨ Written as: r ∩ s
¨ r ∩ s = r – (r – s)
r – s = the rows in r, but not in s
r – (r – s) = the rows in both r and s
¨ Relations must have compatible schemas
¨ Example: find all customers with both a loan and a
bank account
Πcust_name(borrower) ∩ Πcust_name(depositor)
Natural Join Operation
33
¨ Simple example:
“Find the names of all customers with loans.”
¨ Result:
Πcust_name(σborrower.loan_id=loan.loan_id(borrower × loan))
¨ Rewritten with natural join:
Πcust_name(borrower loan)
Natural Join Characteristics
36
¨ Note:
¤ Even though these expressions are equivalent, order of join
operations can dramatically affect query cost!
¤ (Keep this in mind for later…)
Division Operation
37
¨ Binary operator: r ÷ s
¨ Implements a “for each” type of query
¤ “Find all rows in r that have one row corresponding to
each row in s.”
¤ Relation r divided by relation s
¨ Easiest to illustrate with an example:
¨ Puzzle Database
puzzle_list(puzzle_name)
n Simple list of puzzles by name
completed(person_name, puzzle_name)
n Records which puzzles have been completed by each person
Puzzle Database
38
person_name puzzle_name
Alex altekruse
For completed ÷ puzzle_list Alex soma cube
¨ Schemas are compatible Bob puzzle box
Carl altekruse
¨ Result has schema (person_name) Bob soma cube
¤ Attributes in completed schema, but Carl puzzle box
not also in puzzle_list schema Alex puzzle box
Carl soma cube
person_name
completed = r
Alex
Carl
puzzle_name
completed ÷ puzzle_list
altekruse
¨ Every tuple t in result satisfies these soma cube
conditions: puzzle box
t ∈ ΠR–S(r) puzzle_list = s
⟨ ∀ts ∈ s : ∃tr ∈ r : tr[S] = ts[S] ∧ tr[R–S] = t ⟩
Division Operation
42