L8: Relational Algebra Flashcards

1
Q

5 Basic Relational Algebra Operators

A
  • Selection
    • σ (sigma)
  • Projection
    • Π (capital pi)
  • Cross Product
    • X
  • Union
  • Difference
    • -
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is Relational Algebra useful?

A

Relational Algebra allows us to translate

declarative (SQL) queries

into

precise and optimizable expressions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Considerations in

Relational Algebra Formalism

A
  • Where RDBMS consider multisets, Relational Algebra considers sets
  • We consider the Named Perspective
    • Every attribute must have a unique name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Relational Algebra Symbols:

σc ( R )

A

σc ( R )

Selection

Return all tuples in relation R

which match condition C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Relational Algebra Symbols:

ΠA1, … , An ( R )

A

ΠA1, … , An ( R )

Projection

From relation R,

Return tuples only with the attributes

specified by A1, …, An

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Relational Algebra Symbols:

R1 X R2

A

R1 X R2

Cross Product of R1 and R2

Each tuple in R1 with each tuple from R2

Rare, mainly used to express joins

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Relational Algebra Symbols:

R ∪ S

A

R ∪ S

Union of Relations R and S

Returns a relation instance with all tuples from R and S

R and S must be union-compatible,

  • Same number of fields
  • Fields have same domains
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Relational Algebra Symbols:

R - S

A

R - S

Difference of R and S

Returns all tuples that are in R, but not in S

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Relational Algebra Symbols:

R ∩ S

A

R ∩ S

Intersection of R and S

  • Tuples that are in both R and S
  • It is a derived operation:
    • R ∩ S = R - (R - S)
  • R and S must be union compatible
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Relational Algebra:

Derived/Auxiliary Operators

A
  • Intersection
  • Joins
    • Types:
      • Condition
      • Natural
      • equi-join
  • Renaming
    • ρ
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When are Renaming Operations useful?

A
  • Resolving Conflicts
    • Allows you to explicitly give names to some fields
    • example:
      • S1 X R1, there are two fields with the same name
  • Helps break up large expressions
    • When an expression gets long and nested, it can be confusing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Renaming Operation Syntax

A

S( R(F), E )

where:

E : a relational algebra expression (input)

R: The name of the new relation for E (output)

F: renaming list, such as

oldname → newname or

position → newname or

Field names in R are the same as in E,

if they are not renamed in F

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Join Operation

A

Join ⋈ is one of the most useful operations.

Used most commonly to combine information from two or more relations.

Derived Operation:

Join = Cross Product → Selection & Projection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Join Operation:

Benefits of using Join Operation

instead of Cross Product

A
  • Joins used much more than cross-product
  • Result of cross product is much larger than join
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Condition Join:

R ⋈c S

A

R ⋈c S

Condition c can refer to attributes of both R and S

Resulting schema is the same as that of a cross product

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Natural Join :

R ⋈ S

A

R ⋈ S

Special Case of Join:

  • An equi-join on which equalities are specified on all fields having the same name in R and S
  • Result is guaranteed to have no two fields with the same name
17
Q

Equijoin:

R1c S1

A

A special case of Condition Join,

where the condition C only contains equalities

example:

R1c S1 = R1 ⋈s1.sid=r1.sid S1

Results in a schema similar to cross-product, but only one copy of fields for which equality is specified.

Can be thought of as “Join On”

18
Q

Precedence of Relational Operators

A
  1. Select, Project, Rename [σ ,Π, ρ] (highest)
  2. Cross-product, Join [X, ⋈]
  3. Intersect [∩]
  4. Union, difference [U, -]
19
Q

RDBMS Architecture:

SQL Engine Steps

A
  • SQL Query
    • Declaration Query (from User)
  • Relational Algebra (RA) Plan
    • Translate to relational algebra expression
  • Optimized RA Plan
    • Find a logically equivalent, but more efficient RA expression
  • Execution
    • Execute each operator of the optimized plan