# Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.

## Presentation on theme: "Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query."— Presentation transcript:

Relational Algebra Dashiell Fryer

What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query language. It consists of the select, project, union, set difference, Cartesian product, and rename operations. It consists of the select, project, union, set difference, Cartesian product, and rename operations. Set intersection, division, natural join, and assignment combine the fundamental operations. Set intersection, division, natural join, and assignment combine the fundamental operations.

Select Operation Select operation returns a relation that satisfies the given predicate from the original relation. Select operation returns a relation that satisfies the given predicate from the original relation. Denoted by lowercase sigma σ. Denoted by lowercase sigma σ. Example: σ branch-name = “Perryridge” (loan) from page 89 returns a relation of the tuples from relation loan whose branch is Perryridge. Example: σ branch-name = “Perryridge” (loan) from page 89 returns a relation of the tuples from relation loan whose branch is Perryridge. =,≠,≤,≥,, Λ,¬,v are legal in the predicate. =,≠,≤,≥,, Λ,¬,v are legal in the predicate.

Project Unary operation Unary operation Denoted by uppercase pi Π. Denoted by uppercase pi Π. Returns a relation with only the specified attributes. Returns a relation with only the specified attributes. Example: page 90 Π loan-number, amount (loan) lists all of the loan numbers and the amounts. Example: page 90 Π loan-number, amount (loan) lists all of the loan numbers and the amounts.

Composition of Operations Results of relational operations are relations themselves. Results of relational operations are relations themselves. Compositions of operations form a relational-algebra expression. Compositions of operations form a relational-algebra expression.

Union Denoted, as in set theory, by . Denoted, as in set theory, by . Binary operation Binary operation Results in a relation with all of the tuples that appear in either or both of the argument relations. Results in a relation with all of the tuples that appear in either or both of the argument relations.

Union (Cont’d) Unions must be between compatible relations Unions must be between compatible relations Both relations must have the same number of attributes. Both relations must have the same number of attributes. Domains of the ith attribute of the first and the ith attribute of the second must be the same for all i. Domains of the ith attribute of the first and the ith attribute of the second must be the same for all i.

Set Difference Denoted by - Denoted by - Binary operation Binary operation R – S produces all tuples in R but not in S R – S produces all tuples in R but not in S Relations must be compatible under the same conditions as the union operation. Relations must be compatible under the same conditions as the union operation.

Cartesian-Product Denoted by a cross (×) Denoted by a cross (×) Binary operation Binary operation Combines any two relations Combines any two relations R × S is the Cartesian-Product of R and S R × S is the Cartesian-Product of R and S Output has the attributes of both relations Output has the attributes of both relations

Cartesian-Product (Cont’d) Repeated attribute names are preceded by the relation they originated from. Repeated attribute names are preceded by the relation they originated from. Example: page 93 r= borrower × loan Example: page 93 r= borrower × loan (borrower.customer-name, (borrower.customer-name, borrower.loan-number, loan.loan-number, loan.branch-name, loan.amount)

Rename Unary operation Unary operation Denoted by lowercase Greek letter rho ρ Denoted by lowercase Greek letter rho ρ Useful for naming the unnamed relations returned from other operations. Useful for naming the unnamed relations returned from other operations.

Formal Definition Relational Algebra acts on either a relation in the database or a constant relation. Relational Algebra acts on either a relation in the database or a constant relation. Suppose E 1 and E 2 are relational-algebra expressions Suppose E 1 and E 2 are relational-algebra expressions

Formal Definition (Cont’d) All relational-algebra expressions are: All relational-algebra expressions are: E1  E2E1  E2E1  E2E1  E2 E 1 – E 2 E 1 × E 2 σ P (E 1 ), P is the predicate Π S (E 1 ), S is a list of attributes in E 1 ρ x (E 1 ), x is the new name of E 1

Additional Operations The fundamental operations can be composed to make new, simpler operations. The fundamental operations can be composed to make new, simpler operations. Set-Intersection Set-Intersection Natural-Join Natural-Join Division Division Assignment Assignment

Set-Intersection Denoted by the symbol . Denoted by the symbol . Results in a relation that contains only the tuples that appear in both relations. Results in a relation that contains only the tuples that appear in both relations. R  S = R – (R – S) R  S = R – (R – S) Since set-intersection can be written in terms of set-difference, it is not a fundamental operation. Since set-intersection can be written in terms of set-difference, it is not a fundamental operation.

Natural-Join Denoted by |x|. Denoted by |x|. Binary operation Binary operation Creates a Cartesian-product of the arguments then performs selection to force equality on attributes that appear in both relations Creates a Cartesian-product of the arguments then performs selection to force equality on attributes that appear in both relations

Division Denoted by  Denoted by  Binary Operation Binary Operation Used in queries that include the phrase “for all”. Used in queries that include the phrase “for all”.

Division (Cont’d) Division is an operation on schema R – S Division is an operation on schema R – S A tuple t is in r  s if and only if: A tuple t is in r  s if and only if: t is in Π R – S (r) and t is in Π R – S (r) and For every tuple t s in s, there is a tuple t r in r satisfying both of the following: For every tuple t s in s, there is a tuple t r in r satisfying both of the following: a. t r [S] = t s [R] b. t r [R – S] = t

Assignment Denoted by the  symbol Denoted by the  symbol Works like assignment operations in programming languages Works like assignment operations in programming languages Assignment must always be made to a temporary relation Assignment must always be made to a temporary relation Does not result in display to the user Does not result in display to the user

Summary Relational Algebra defines a set of operations that operate on relations and result in new tables. Relational Algebra defines a set of operations that operate on relations and result in new tables. Operations can be composed Operations can be composed Additional operations simplify the algebra but do not add any additional power. Additional operations simplify the algebra but do not add any additional power.

The End

Download ppt "Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query."

Similar presentations