21
Relational Algebra Relational Algebra Dashiell Fryer Dashiell Fryer

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

Embed Size (px)

Citation preview

Relational AlgebraRelational Algebra

Dashiell FryerDashiell Fryer

What is Relational Algebra?What is Relational Algebra?

Relational algebra is a procedural Relational algebra is a procedural query language.query language.

It consists of the select, project, It consists of the select, project, union, set difference, Cartesian union, set difference, Cartesian product, and rename operations.product, and rename operations.

Set intersection, division, natural Set intersection, division, natural join, and assignment combine the join, and assignment combine the fundamental operations.fundamental operations.

Select OperationSelect Operation

Select operation returns a relation that Select operation returns a relation that satisfies the given predicate from the satisfies the given predicate from the original relation.original relation.

Denoted by lowercase sigma Denoted by lowercase sigma σσ.. Example: Example: σσbranch-name = “Perryridge”branch-name = “Perryridge”(loan) from page (loan) from page

89 returns a relation of the tuples from 89 returns a relation of the tuples from relation relation loanloan whose branch is Perryridge. whose branch is Perryridge.

=,≠,≤,≥,<,>, =,≠,≤,≥,<,>, ΛΛ,¬,v are legal in the ,¬,v are legal in the predicate.predicate.

ProjectProject

Unary operationUnary operation Denoted by uppercase pi Denoted by uppercase pi ΠΠ.. Returns a relation with only the Returns a relation with only the

specified attributes.specified attributes. Example: page 90 Example: page 90 ΠΠloan-number, amount loan-number, amount (loan) (loan)

lists all of the loan numbers and the lists all of the loan numbers and the amounts.amounts.

Composition of OperationsComposition of Operations

Results of relational operations are Results of relational operations are relations themselves.relations themselves.

Compositions of operations form a Compositions of operations form a relational-algebra expression.relational-algebra expression.

UnionUnion

Denoted, as in set theory, by Denoted, as in set theory, by .. Binary operationBinary operation Results in a relation with all of the Results in a relation with all of the

tuples that appear in either or both tuples that appear in either or both of the argument relations.of the argument relations.

Union (Cont’d)Union (Cont’d)

Unions must be between compatible Unions must be between compatible relationsrelations

Both relations must have the same Both relations must have the same number of attributes.number of attributes.

Domains of the Domains of the iith attribute of the th attribute of the first and the first and the iith attribute of the th attribute of the second must be the same for all second must be the same for all i.i.

Set DifferenceSet Difference

Denoted by -Denoted by - Binary operationBinary operation R – S produces all tuples in R but not R – S produces all tuples in R but not

in Sin S Relations must be compatible under Relations must be compatible under

the same conditions as the union the same conditions as the union operation.operation.

Cartesian-ProductCartesian-Product

Denoted by a cross (×)Denoted by a cross (×) Binary operationBinary operation Combines any two relationsCombines any two relations R × S is the Cartesian-Product of R R × S is the Cartesian-Product of R

and Sand S Output has the attributes of both Output has the attributes of both

relationsrelations

Cartesian-Product (Cont’d)Cartesian-Product (Cont’d)

Repeated attribute names are preceded Repeated attribute names are preceded by the relation they originated from.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-borrower.loan-number, loan.loan-number, loan.branch-name, number, loan.branch-name, loan.amount)loan.amount)

RenameRename

Unary operationUnary operation Denoted by lowercase Greek letter Denoted by lowercase Greek letter

rho rho ρρ Useful for naming the unnamed Useful for naming the unnamed

relations returned from other relations returned from other operations.operations.

Formal DefinitionFormal Definition

Relational Algebra acts on either a Relational Algebra acts on either a relation in the database or a constant relation in the database or a constant relation.relation.

Suppose ESuppose E1 1 and E and E22 are relational- are relational-algebra expressionsalgebra expressions

Formal Definition (Cont’d)Formal Definition (Cont’d)

All relational-algebra expressions are:All relational-algebra expressions are:EE11 E E22

EE11 – E – E22

EE11 × E × E22

σσPP(E(E11), P is the predicate), P is the predicateΠΠSS(E(E11), S is a list of attributes in E), S is a list of attributes in E11

ρρxx(E(E11), x is the new name of E), x is the new name of E11

Additional OperationsAdditional Operations

The fundamental operations can be The fundamental operations can be composed to make new, simpler composed to make new, simpler operations.operations.

Set-IntersectionSet-Intersection Natural-JoinNatural-Join DivisionDivision AssignmentAssignment

Set-IntersectionSet-Intersection

Denoted by the symbol Denoted by the symbol .. Results in a relation that contains Results in a relation that contains

only the tuples that appear in both only the tuples that appear in both relations.relations.

R R S = R – (R – S) S = R – (R – S) Since set-intersection can be written Since set-intersection can be written

in terms of set-difference, it is not a in terms of set-difference, it is not a fundamental operation.fundamental operation.

Natural-JoinNatural-Join

Denoted by |x|.Denoted by |x|. Binary operationBinary operation Creates a Cartesian-product of the Creates a Cartesian-product of the

arguments then performs selection arguments then performs selection to force equality on attributes that to force equality on attributes that appear in both relationsappear in both relations

DivisionDivision

Denoted by Denoted by Binary OperationBinary Operation Used in queries that include the Used in queries that include the

phrase “for all”.phrase “for all”.

Division (Cont’d)Division (Cont’d)

Division is an operation on schema R – Division is an operation on schema R – S S

A tuple t is in r A tuple t is in r s if and only if: s if and only if: t is in t is in ΠΠR – SR – S(r) and(r) and For every tuple tFor every tuple tss in s, there is a tuple t in s, there is a tuple trr

in r satisfying both of the following:in r satisfying both of the following:

a. ta. trr[S] = t[S] = tss[R][R]

b. tb. trr[R – S] = t[R – S] = t

AssignmentAssignment

Denoted by the Denoted by the symbol symbol Works like assignment operations in Works like assignment operations in

programming languagesprogramming languages Assignment must always be made to Assignment must always be made to

a temporary relationa temporary relation Does not result in display to the user Does not result in display to the user

SummarySummary

Relational Algebra defines a set of Relational Algebra defines a set of operations that operate on relations operations that operate on relations and result in new tables.and result in new tables.

Operations can be composedOperations can be composed Additional operations simplify the Additional operations simplify the

algebra but do not add any additional algebra but do not add any additional power.power.

The EndThe End