Database Relational Algebra

#Relational Algebra

  • Procedural language
  • Based on relational operations
  • Three set of RA operations
    • Basic RA operations
      • select, project, union, set diff, cartesian product, rename
    • Additional RA operations
      • Can be expressed using basic ones
      • But make it easier to write queries
      • intersection, assignment joins, division
    • Extended RA
      • Increases power of RA
      • Cannot be expressed with basic RA
      • Aggregation and group_by

#Relational Operators

Six basic operators:

All operators take one or two relations as input and produce a new relation as a result Addition operators defined on top of these six

#Basic RA operators
#select
σA = B and D > 5(r) \huge \sigma_{\text{A = B and D > 5}}(r)
#project
ΠA,C(r) \huge \Pi_{A, C} (r)
#union
r∪s \huge r \cup s
#set difference
r−s \huge r - s
#Cartesian Product
r×s \huge r \times s
#composition of operations

Can build expressions using multiple operations Example: σA=C(r×s)\sigma_{A = C}(r \times s)

#rename

Allow us to rename, and therefore to refer to, the results of relational algebra expressions.

Allow us to refer to a relation by more than one name.

example: ρX(E)\rho_X(E) returns the expression E under the name X

If a relation-algebra expression E has arity n, then

ρX(A1,A2,⋯ ,An)(E) \large \rho_{X(A_1, A_2, \cdots, A_n)}(E)

returns the result of expression E under the name X, and with the attributes renamed to A1,A2,⋯ ,AnA_1, A_2, \cdots, A_n

Example: Give relation CAKE(cname, price, slices)

ρcheapcake(cakename,price,slices)(σprice<10(CAKE)) \large \rho_{cheapcake(cakename, price, slices)}(\sigma_{price < 10}(CAKE))
#Formal Definition

A basic expression in the relational algebra consists of either one of the following:

  • A relation in the database
  • A constant relation

Let E1E_1 and E2E_2 be relational-algebra expressions; the following are all relational-algebra expressions:

E1∪E2 E_1 \cup E_2

E1−E2 E_1 - E_2

E1×E2 E_1 \times E_2

σp(E1) \sigma_{p}(E_1)

Πs(E1) \Pi_{s}(E_1)

ρX(E1) \rho_{X}(E_1)

#Additional Operations

We define additional operations that do not add any power to the relational algebra, but that simplify common queries

#set intersection
r∩s \huge r \cap s
#natural Join

Let r and s be relations on schemas R and S respectively. Then, the "natural join" of relations R and S is a relation on schema R∪SR \cup S obtained as follows:

  • Consider each pair of tuples trt_r from r and tst_s from s
  • If trt_r and tst_s have the same value on each of the attributes in R∩SR \cap S, add a tuple tt to the result, where
    • tt has the same value as trt_r on r
    • tt has the same value as tst_s on s
#natural join and theta join
#assignment

The assignment operation (←\leftarrow) provides a convenient way to express conples queries.

Write query as sequential program consisting of

  • a series of assignments
  • followed by an operation whose value is displayed as a result of the query

Assignment must always be made to a temporary relation variable

Motivation from algebra

#outer join

An extension of the join operation that avoids loss of information

Computes the join and then adds tuples from one relation that does not match tuples in the other relation to the result of the join

Uses null values:

  • null signifies that hte value is unknown or does not exist
  • All comparisons involving null are(roughly speaking) false by definition
#Different join: theta vs natural vs outer join

Demo relation: Customer(cid, cname, joindate) purchase(cid, pid, buydate)

theta join: more general than natural join

natural join: special case, join on common attributes

works 95% of time, but what is we use "date" in both?

inner versus outer join is separate issue

#null values
  • It is possible for tuples to have a null value, denoted by null, for some of their attributes
  • null signifies an unknown value or that a value does not exist(yet)
  • The result of any arithmetic expression involving null is null.
  • Aggregate functions simply ignore null values
  • For duplicate elimination and grouping, null is treated like any other values, and two nulls are assumed to be the same
  • Comparisons with null values return the special truth value: unknown
  • Three-valued login using the truth value unknown:
    • or:
    • (unknown or true) = true
    • (unknown or false) = unknown
    • (unknown or unknown) = unknown
    • and;
    • (unknown and true) = unknown
    • (unknown and false) = false
    • (unknown and unknown) - unknown
    • not:
      • (not unknown) = unknown
#division

Given relation r(R) and s(S), such that S⊂RS \subset R, r÷sr \div s is the largest relation t(R - S) such that t×S⊆rt \times S \subseteq r

Example:

let

r(ID,courseid)=ΠID,courseid(takes) \large r(ID, course_id) = \Pi_{ID, course_id}(takes)

and

s(courseid)=Πcourseid(σdept_name = ’Biology’(course)) \large s(course_id) = \Pi_{course_id}(\sigma_{\text{dept\_name = 'Biology'}}(course))

then r÷sr \div s gives us students who have taken all courses offered by the Biology department.

Can write r÷sr \div s as

temp1←ΠR−S(r)temp2←ΠR−S((temp1×s)−ΠR−S,S(r))result=temp1−temp2 temp1 \leftarrow \Pi_{R - S}(r) temp2 \leftarrow \Pi_{R - S}((temp1 \times s) - \Pi_{R - S, S}(r)) result = temp1 - temp2
#Overview

#Extended relation algebra

#generalized projection

Extends the projection operation by allowing arithmetic functions to be used in the projection list

ΠF1,F2,⋯ ,Fn(E) \large \Pi_{F_1, F_2, \cdots, F_n}(E)

E is any relational-algebra expression Each of F1,F2,⋯ ,FnF_1, F_2, \cdots, F_n are arithmetic expressions involving constants and attributes in the schema of E

Example: Given relation instructor(ID, name, dept_name, salary) where salary is annual salary, get the same information but with monthly salary

ΠID, name, dept_name, salary/12(instructor) \large \Pi_{\text{ID, name, dept\_name, salary/12}}(instructor)
#aggregate functions and operations
  • Aggregate function takes a collection of values and returns a single values as result

    • avg: average value
    • min: minium value
    • max: maximum value
    • sum: sum of values
    • count: number of values
  • Aggregate operation in relational algebra

G1,G2,⋯ ,GnGF1(A1),F2(A2),⋯ ,Fn(An)(E) \large _{G_1, G_2, \cdots, G_n} \mathcal{G} _{F_1(A_1), F_2(A_2), \cdots, F_n(A_n)}(E)

E is any relational algebra operation

  • G1,⋯ ,GnG_1, \cdots, G_n is a list of attributes on which to group(can my empty)
  • Each FiF_i is an aggregate function
  • each AiA_i is an attribute name

Note: some books/articles use γ\gamma instead of G\mathcal{G}

  • Result of aggregation does not have a name
    • Can use rename operation to give it a name
    • For convenience, we permit renaming as part of aggregate operation

#Multiset Relational Algebra

Pure relational algebra removes all duplicates.

Multiset relational algebra retains duplicates, to match SQL semantics

  • SQL duplicate retention was initially for efficiency, but is now a feature

Multiset relational algebra defined as follows

  • selection: has as many duplicates of a tuple as in the input, if the tuple satisfies the selection
  • projection: one tuple per input tuple, even if ie is a duplicate
  • cross product: if there are m copies of t1 in r, and n copies of t2 in s, there are m x n copies of t1.t2 in r x s.
  • other operators similarly defined
#RA examples