profile pic
⌘ '
raccourcis clavier
OperatorOperationExample
σC\sigma_CSelectionσA=10(R)\sigma_{A=10}(R)
πL\pi_LProjectionπA,B(R)\pi_{A,B}(R)
×\timesCross-ProductR1×R2R_1 \times R_2
\bowtieNatural JoinR1R2R_1 \bowtie R_2
C\bowtie_CTheta JoinR1R1.A=R2.AR2R_1 \bowtie_{R_1.A=R_2.A} R_2
ρR\rho_RRenameρS(R)\rho_S(R)
δ\deltaEliminate Duplicatesδ(R)\delta(R)
τ\tauSort Tuplesτ(R)\tau(R)
γL\gamma_LGrouping & AggregationγA,AVG(B)(R)\gamma_{A,AVG(B)}(R)

selection

idea: picking certain row

R1σC(R2)R_{1} \coloneqq \sigma_C(R_{2})

CC is the condition refers to attribute in R2R_{2}

def: R1R_{1} is all those tuples of R2R_{2} that satisfies C

projection

idea: picking certain column

R1πL(R2)R_{1} \coloneqq \pi_L(R_{2})

LL is the list of attributes from R2R_{2}

R=[AB1234]πA+BC,AA1,AA2(R)=[CA1A2311733]\begin{aligned} R &= \begin{bmatrix} A & B \\ 1 & 2 \\ 3 & 4 \end{bmatrix} \\[8pt] \pi_{A+B \rightarrow C, A \rightarrow A_1, A \rightarrow A_2}(R) &= \begin{bmatrix} C & A_1 & A_2 \\ 3 & 1 & 1 \\ 7 & 3 & 3 \end{bmatrix} \end{aligned}

products

R3R1×R2R_{3} \coloneqq R_{1} \times R_{2}

theta-join

R3R1CR2R_{3} \coloneqq R_{1} \bowtie_C R_{2}

idea: product of R1R_{1} and R2R_{2} then apply σC\sigma_C to results

think of AΘBA \Theta B where Θ=,<, etc.\Theta \coloneqq =, <, \text{ etc.}

natural join

R3R1R2R_{3} \coloneqq R_{1} \bowtie R_{2}
  • equating attributes of the same name
  • projecting out one copy of each pair of equated attributes

renaming

R1ρR1(A1,,An)(R2)R_{1} \coloneqq \rho_{R_{1}(A_{1},\ldots,A_n)}(R_{2})

set operators

union compatible

two relations are said to be union compatible if they have the same set of attributes and types (domain) of the attributes are the same

i.e: Student(sNumber, sName) and Course(cNumber, cName) are not union compatible

bags

definition

modification of a set that allows repetition of elements

Think of {1,2,3,1,1}\{1,2,3,1,1\} is a bags, whereas {1,2,3}\{1,2,3\} is also considered a bag.

in a sense, {1,2,3}\{1,2,3\} happens to also be a set.

Lien vers l'original

Set Operations on Relations

For relations R1R_1 and R2R_2 that are union compatible, here’s how many times a tuple tt appears in the result:

OperationSymbolResult (occurrences of tuple tt)
Union\cupm+nm + n
Intersection\capmin(m,n)\texttt{min}(m,n)
Difference-max(0,mn)\texttt{max}(0, m-n)

where mm is the number of times tt appears in R1R_1 and nn is the number of times it appears in R2R_2.

sequence of assignments

precedence of relational operators:

σπρ×\begin{aligned} &\sigma \quad \pi \quad \rho \\[8pt] & \times \quad \bowtie \\[9pt] & \cap \\ &\cup \quad - \end{aligned}

expression tree

extended algebra

δ\delta: eliminate duplication from bags

τ\tau: sort tuples

γL(R)\gamma_{L}(R) grouping and aggregation

outerjoin: avoid dangling tuples

duplicate elimination

δ(R)\delta(R)

Think of it as converting it to set

sorting

τL(R)\tau_L(R)

with LL is a list of some attributes of RR

basically for ascending order, for descending order then use τL,DESC(R)\tau_{L, \text{DESC}}(R)

applying aggregation

or γL(R)\gamma_{L}(R)

  • group RR accordingly to all grouping attributes on LL

  • per group, compute AGG(A) for each aggrgation on LL

  • result has one tuple for each group: grouping attributes and aggregations

aggregation is applied to an entire column to produce a single results

outerjoin

essentially padding missing attributes with NULL

bag operations

remember that bag and set operations are different

set union is idempotent, whereas bags union is not.rightarrow

bag union: {1,2,1}{1,1,2,3,1}={1,1,1,1,1,2,2,3}\{1,2,1\} \cup \{1,1,2,3,1\} = \{1,1,1,1,1,2,2,3\}

bag intersection: {1,2,1,1}{1,2,1,3}={1,1,2}\{1,2,1,1\} \cap \{1,2,1,3\} = \{1,1,2\}

bag difference: {1,2,1,1}{1,2,3}={1,1}\{1,2,1,1\} - \{1,2,3\} = \{1,1\}