Review of the Relational Algebra 5DV120 Database System Principles - - PowerPoint PPT Presentation

review of the relational algebra
SMART_READER_LITE
LIVE PREVIEW

Review of the Relational Algebra 5DV120 Database System Principles - - PowerPoint PPT Presentation

Review of the Relational Algebra 5DV120 Database System Principles Ume a University Department of Computing Science Stephen J. Hegner hegner@cs.umu.se http://www.cs.umu.se/~hegner Review of the Relational Algebra 20130526 Slide 1 of


slide-1
SLIDE 1

Review of the Relational Algebra

5DV120 — Database System Principles Ume˚ a University Department of Computing Science Stephen J. Hegner hegner@cs.umu.se http://www.cs.umu.se/~hegner

Review of the Relational Algebra 20130526 Slide 1 of 13

slide-2
SLIDE 2

About these Slides

  • These slides are mostly adapted from those for 5DV119 by the same

instructor.

  • The adaptations use the schema of the Silberschatz-Korth-Sudarsham

textbook, rather than that of the Elmasri-Navathe textbook.

  • These slides will be used as a refresher/review, and will be covered much

more rapidly than in the introductory course.

  • They are intended for those who already know/knew the relational

algebra, but need a quick refresher.

Review of the Relational Algebra 20130526 Slide 2 of 13

slide-3
SLIDE 3

Overview of the Relational Algebra

  • The relational algebra is defined in terms of three kinds of operations on

relations: Operations specific to relations: Projection: Trim some columns from a relation. Selection: Trim some rows from a relation. Join: Combine two relations by matching values. The three fundamental set-theoretic operations: Union: X ∪ Y = all elements in either X or Y . Intersection: X ∩ Y = all elements in both X and Y . Difference: X \ Y or X − Y = all elements in X which are not in Y . A special operation: Attribute renaming: Change the names of some attributes of a relation.

Review of the Relational Algebra 20130526 Slide 3 of 13

slide-4
SLIDE 4

Projection

  • The projection operation takes a “vertical” slice of a relation by dropping

some columns while retaining others.

  • The projection operator is represented by the lowercase Greek letter π,

with the subscript identifying the columns to be retained. π{A1,A2,...,Ak}(R)

  • The semantics of this expression are exactly those of the following SQL

query. SELECT DISTINCT A1, A2, . . . , Ak FROM R;

  • This is a formal operation on sets; duplicates are not part of the model.
  • Often, the set brackets are dropped in the subscript.

πA1,A2,...,Ak(R)

  • If the attribute names are single letters, even the commas are sometimes

dropped. πA1A2...Ak(R)

Review of the Relational Algebra 20130526 Slide 4 of 13

slide-5
SLIDE 5

Selection

  • The selection operation takes a “horizontal” slice of a relation by

dropping some rows while retaining others.

  • The selection operator is represented by the lowercase Greek letter σ,

with the subscript containing an expression which identifies the rows to be retained. σϕ(R)

  • The semantics of this expression are exactly those of the following SQL

query. SELECT DISTINCT * FROM R WHERE ϕ;

  • The expression ϕ is often written in a more formal, logical style than that

used by SQL. Example: σ((dept name=′Comp. Sci.′)∧(salary≥70000))(instructor);

Review of the Relational Algebra 20130526 Slide 5 of 13

slide-6
SLIDE 6

Combining Expressions in the Relational Algebra

  • The operations in the relational algebra themselves produce relations as

results.

  • Therefore, they may be composed.

Example: πA1,A2,...,Ak(σϕ(R)) has the same meaning as

SELECT DISTINCT A1, A2, . . . , Ak FROM R WHERE ϕ;

  • Typing rules must be observed, since it is the composition of two distinct
  • perations.

Example: While πname,dept name(σsalary≥70000(instructor)) makes perfect sense, σsalary≥30000(πname,dept name(instructor)) does not.

Review of the Relational Algebra 20130526 Slide 6 of 13

slide-7
SLIDE 7

Assignment Programs in the Relational Algebra

  • Instead of composing operations in functional notation, queries in the

relational algebra may be expressed as a sequence of assignment statements. Example: The functional composition πname,dept name(σsalary≥70000(instructor)) may also be expressed as the program of assignments X1 ← −σsalary≥70000(instructor) X2 ← −πname,dept name(X1) with X2 as the final result.

  • It is often easier to read and follow such sequence of assignments than to

read and follow a complex functional composition.

Review of the Relational Algebra 20130526 Slide 7 of 13

slide-8
SLIDE 8

Join

  • The join is a binary operation represented by the “bowtie” symbol .
  • It is basically the inner join of SQL.
  • There are, however, a number of variants depending upon the subscript

(or lack thereof).

  • The expression

R1 ϕ R2 has the semantics of the SQL expression

SELECT * FROM R_1 JOIN R_2 ON (ϕ);

provided ϕ is represented in the correct way. Example: instructor (instructor.dept name=department.dept name) department has the meaning of

SELECT * FROM instructor JOIN department ON (instructor.dept_name=department.dept_name );

Review of the Relational Algebra 20130526 Slide 8 of 13

slide-9
SLIDE 9

Further Join Conventions

  • Multiple conditions may be shown in various ways:

instructor (instructor.dept name=department.dept nme)∧(salary≥budget) department instructor {(instructor-dept name=department.dept name),(salary≥budget)} department instructor (instructor.dept name=department.dept name),(salary≥budget) department

  • These all have the meaning of

SELECT * FROM instructor JOIN department ON (( instructor .dept_name= department.dept_name) AND (salary >= budget ));

  • Other logical connectives:

instructor (instructor.dept name=department.dept nme)∨(salary≥budget) department has the following meaning.

SELECT * FROM instructor JOIN department ON (( instructor .dept_name= department= department .dept_name) OR (salary >= budget ));

Review of the Relational Algebra 20130526 Slide 9 of 13

slide-10
SLIDE 10

Natural and Cross Joins

  • The natural join is indicated by the absence of any subscripts on .
  • The textbook of Elmasri and Navathe uses the ∗ symbol for natural join,

although this notation is rather dated.

  • The expression

department teaches has the same meaning as

SELECT * FROM department NATURAL JOIN teaches;

  • Note that ∅ is the cross join, with no matches. (∅ = {} = empty set.)
  • Thus, department ∅ teaches has the meaning of

SELECT * FROM department JOIN teaches ON (TRUE );

  • This cross join (or Cartesian product) is also denoted

department × teaches.

Review of the Relational Algebra 20130526 Slide 10 of 13

slide-11
SLIDE 11

Renaming

  • Recall that it is sometimes necessary to have multiple copies of the same

relation. Query: Find the pairs of IDs for instructors who work in the same department.

SELECT A.ID , B.ID FROM instructor as A JOIN instructor as B ON (A.dept_name=B.dept_name );

  • In the relational algebra, there is a rename operation to support this.
  • There are two main formats:
  • ρR′(R) returns a copy of R named R′, with the same attribute

names.

  • ρR′(A′

1,A′ 2,...,A′ k)(R) returns a copy of R named R′, with the attributes

renamed to A′

1, A′ 2, . . . , A′ k.

  • Name qualifiers are used as in SQL.
  • However, the original relation does not require a qualifier.

Review of the Relational Algebra 20130526 Slide 11 of 13

slide-12
SLIDE 12

Renaming Example

Query: Find the pairs of IDs for instructors who work in the same department.

  • The above query as a sequence of steps in the relational algebra, with X3

the answer, using each of the renaming conventions: X1 ← −ρA(instructor) X2 ← −ρB(instructor) X3 ← −X1 (A.dept name=B.dept name) X2 X4 ← −πA.ID,B.ID(X3) X1 ← −ρB(ID′,name′.dept name′,salary′)(instructor) X2 ← −instructor (dept name=dept name’) (X1) X3 ← −πID,ID′(X2)

Review of the Relational Algebra 20130526 Slide 12 of 13

slide-13
SLIDE 13

Set Operations

  • The following set operations are considered part of the relational algebra:

Union: X ∪ Y = all elements in either X or Y . Intersection: X ∩ Y = all elements in both X and Y . Difference: X \ Y or X − Y = all elements in X which are not in Y .

  • They may only be applied when the elements in each set are of the same

type.

  • If they are tuples, they have the same number of columns.
  • The attributes for matching columns must be of the same type.

Review of the Relational Algebra 20130526 Slide 13 of 13