Relational Algebra Ii: CS121: Relational Databases Fall 2018 - Lecture 3
Relational Algebra Ii: CS121: Relational Databases Fall 2018 - Lecture 3
ALGEBRA
CS121: II
Relational Databases
Fall 2018 – Lecture 3
Last
2
Lecture
Query languages provide support for retrieving
information from a database
Introduced the relational algebra
¤ A procedural query language
¤ Six fundamental operations:
select, project, set-union, set-
difference, Cartesian product, rename
¤ Several additional operations, built upon the
fundamental operations
set-intersection, natural join, division,
assignment
Extended Operations
3
person_nameGcount(puzzle_name)(completed)
completed_times
General form: G , G , …, G
1 2 n F1 (A1), F2 (A2 ), …, Fm
(E
Gevalutes to a relation
¤ E (A )m
)
¤ Leading Gi are attributes of E to group on
¤ Each Fj is aggregate function applied to attribute Aj of E
First, input relation is divided into groups
¤ If no attributes Gi specified, no grouping is
performed (it’s just one big group)
Then, aggregate functions applied to each group
General Form of Aggregates (2)
16
General form: G , G , …, G F (A ), F (A ), …, F (E
1 2 n 1 1 2 2 m
G (A )m
Tuples in E are grouped such that: )
¤ All tuples in a group have same values for attributes
G1, G2, …, Gn
¤ Tuples in different groups have different values for
G1, G2, …, Gn
Thus, the values {g1, g2, …, gn} in each
group uniquely identify the group
¤ {G1, G2, …, Gn} are a superkey for the result
relation
General Form of Aggregates (3)
17
General form: G , G , …, G F (A ), F (A ), …, F (E
1 2 n 1 1 2 2 m
G in result (Ahave
Tuples ) m
the form: )
{g1, g2, …, gn, a1, a2, …, am}
¤ gi are values for that particular group
¤ aj is result of applying Fj to the multiset of values of
Aj
in that group
Important note: Fj(Aj) attributes are unnamed!
¤ Informally we refer to them as Fj(Aj) in results,
but they have no name.
¤ Specify a name, same as before: Fj(Aj) as
attr_name
One More Aggregation Example
18
Project: (E)
¤ Project operation must eliminate duplicates
¤ null value is treated like any other value
¤ Duplicate tuples containing null values are also
eliminated
Union, Intersection, and Difference
¤ null values are treated like any other value
¤ Set union, intersection, difference computed as expected
These choices are somewhat arbitrary
¤ null means “value is unknown or missing”…
¤ …but in these cases, two null values are considered
equal.
¤ Technically, two null values aren’t the same. (oh
Grouping and Aggregation
27
In grouping phase:
¤ null is treated like any other value
¤ If two tuples have same values (including null) on
the grouping attributes, they end up in same group
In aggregation phase:
¤ null values are removed from the input multiset before
the aggregate function is applied!
Slightly different from arithmetic behavior; it keeps one null
value from wiping out an aggregate computation.
¤ If the aggregate function gets an empty multiset for
input, the result is null…
…except for count! In that case, count returns 0.
Generalized Projection, Outer
28
Joins
Generalized Projection operation:
¤ A combination of simple projection and
arithmetic operations
¤ Easy to figure out from previous rules
Outer joins:
¤ Behave just like natural join operation, except
for padding missing values with null
Back to Our person_name puzzle_name
29
Puzzle! Alex
Alex
altekruse
soma cube
“How many people Bob puzzle box
have completed puzzle_name Carl altekruse
altekruse Bob soma cube
each puzzle?” soma cube Carl puzzle box
puzzle box Alex puzzle box
clutch box Carl soma cube
puzzle_list completed
altekruse Bob
soma cube
puzzle Carl
box
Bob
Counting the
30
Solutions
Now, use grouping and aggregation
¤ Group on puzzle name
¤ Count up the people!
completed
puzzle_nameGcount(person_name)(puzzle_list
)
puzzle_name person_name puzzle_name person_name puzzle_name
altekruse Alex altekruse Alex altekruse 2
soma cube soma cube 3
puzzle Alex altekruse Carl puzzle box 3
box soma cube Alex clutch box 0
altekruse Bob soma
soma cube cube Bob
puzzle Carl soma cube
box Carl
Bob
puzzle box Bob
puzzle
Carl
box Carl
Database Modification
31
r r–E altekruse
soma cube
Example puzzle
box puzzle_list
:
person_name puzzle_name
Problem:
Get rid of Alex altekruse
the completed relation
Alex
soma cube
puzzle
“somareferences the puzzle_list box
relation Bob altekruse
cube”
To respect referential integrity soma cube
puzzle.constraints, should delete Carl puzzle
box
from completed first. Bob
completed
Carl
Deleting Tuples
36
(2)
completed references puzzle_list
¤ puzzle_name is a key
¤ completed shouldn’t have any values for puzzle_name
that don’t appear in puzzle_list
¤ Delete tuples from completed first.
¤ Then delete tuples from puzzle_list.
balance≥10000(account)
Updating Some Tuples
40
(2)
Another example:
“Add 5% interest to accounts with a balance less than
$10,000, and 6% interest to accounts with a balance
of $10,000 or more.”
account acct_id,branch_name,balance*1.05(balance<10000(account))
acct_id,branch_name,balance*1.06(balance≥10000(account))