concepts of relational algebra

Upload: syedkareemhkg

Post on 08-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 concepts of Relational Algebra

    1/21

    Relational AlgebraRelational Algebra

    Dashiell FryerDashiell Fryer

  • 8/7/2019 concepts of Relational Algebra

    2/21

    W

    hat is Relational Algebra?W

    hat is Relational Algebra? Relational algebra is a procedural queryRelational algebra is a procedural query

    language.language.

    It consists of the select, project, union, setIt consists of the select, project, union, setdifference, Cartesian product, and renamedifference, Cartesian product, and renameoperations.operations.

    Set intersection, division, natural join, andSet intersection, division, natural join, andassignment combine the fundamentalassignment combine the fundamentaloperations.operations.

  • 8/7/2019 concepts of Relational Algebra

    3/21

    Select OperationSelect Operation Select operation returns a relation that satisfiesSelect operation returns a relation that satisfies

    the given predicate from the original relation.the given predicate from the original relation.

    Denoted by lowercase sigmaDenoted by lowercase sigma ..

    Example:Example: branchbranch--name = Perryridgename = Perryridge(loan) from page 89(loan) from page 89returns a relation of the tuples from relationreturns a relation of the tuples from relation loanloanwhose branch is Perryridge.whose branch is Perryridge.

    =,,,,,=,,,,, ,,v are legal in the predicate.,,v are legal in the predicate.

  • 8/7/2019 concepts of Relational Algebra

    4/21

    ProjectProject Unary operationUnary operation

    Denoted by uppercase piDenoted by uppercase pi

    .. Returns a relation with only the specifiedReturns a relation with only the specified

    attributes.attributes.

    Example: page 90Example: page 90 loanloan--number, amountnumber, amount(loan) lists(loan) listsall of the loan numbers and the amounts.all of the loan numbers and the amounts.

  • 8/7/2019 concepts of Relational Algebra

    5/21

    Composition of OperationsComposition of Operations Results of relational operations areResults of relational operations are

    relations themselves.relations themselves.

    Compositions of operations form aCompositions of operations form arelationalrelational--algebra expression.algebra expression.

  • 8/7/2019 concepts of Relational Algebra

    6/21

    UnionUnion Denoted, as in set theory, byDenoted, as in set theory, by ..

    B

    inary operationB

    inary operation Results in a relation with all of the tuplesResults in a relation with all of the tuples

    that appear in either or both of thethat appear in either or both of theargument relations.argument relations.

  • 8/7/2019 concepts of Relational Algebra

    7/21

    Union (Contd)Union (Contd)

    Unions must be between compatibleUnions must be between compatiblerelationsrelations

    Both relations must have the sameBoth relations must have the samenumber of attributes.number of attributes.

    Domains of theDomains of the iith attribute of the firstth attribute of the first

    and theand the iith attribute of the second mustth attribute of the second mustbe the same for allbe the same for all i.i.

  • 8/7/2019 concepts of Relational Algebra

    8/21

    Set DifferenceSet Difference

    Denoted byDenoted by --

    B

    inary operationB

    inary operation RR S produces all tuples in R but not in SS produces all tuples in R but not in S

    Relations must be compatible under theRelations must be compatible under thesame conditions as the union operation.same conditions as the union operation.

  • 8/7/2019 concepts of Relational Algebra

    9/21

    CartesianCartesian--ProductProduct

    Denoted by a cross (Denoted by a cross ())

    B

    inary operationB

    inary operation Combines any two relationsCombines any two relations

    RR S is the CartesianS is the Cartesian--Product of R and SProduct of R and S

    Output has the attributes of both relationsOutput has the attributes of both relations

  • 8/7/2019 concepts of Relational Algebra

    10/21

    CartesianCartesian--Product (Contd)Product (Contd)

    Repeated attribute names are preceded byRepeated attribute names are preceded bythe relation they originated from.the relation they originated from.

    Example: page 93 r= borrowerExample: page 93 r= borrower loanloan

    (borrower.customer(borrower.customer--name,name,

    borrower.loanborrower.loan--number, loan.loannumber, loan.loan--number,number,loan.branchloan.branch--name, loan.amount)name, loan.amount)

  • 8/7/2019 concepts of Relational Algebra

    11/21

    RenameRename

    Unary operationUnary operation

    Denoted by lowercase Greek letter rhoDenoted by lowercase Greek letter rho

    Useful for naming the unnamed relationsUseful for naming the unnamed relationsreturned from other operations.returned from other operations.

  • 8/7/2019 concepts of Relational Algebra

    12/21

    Formal DefinitionFormal Definition

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

    Suppose ESuppose E11 and Eand E22 are relationalare relational--algebraalgebraexpressionsexpressions

  • 8/7/2019 concepts of Relational Algebra

    13/21

    Formal Definition (Contd)Formal Definition (Contd)

    All relationalAll relational--algebra expressions are:algebra expressions are:

    EE11 EE22

    EE11 EE22

    EE11 EE22

    PP(E

    (E11

    ), P is the predicate), P is the predicateSS(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 ofE), x is the new name ofE11

  • 8/7/2019 concepts of Relational Algebra

    14/21

    Additional OperationsAdditional Operations

    The fundamental operations can beThe fundamental operations can becomposed to make new, simplercomposed to make new, simpler

    operations.operations.

    SetSet--IntersectionIntersection

    NaturalNatural--JoinJoin

    DivisionDivision

    AssignmentAssignment

  • 8/7/2019 concepts of Relational Algebra

    15/21

    SetSet--IntersectionIntersection

    Denoted by the symbolDenoted by the symbol ..

    Results in a relation that contains only theResults in a relation that contains only thetuples that appear in both relations.tuples that appear in both relations.

    RR S = RS = R (R(R S)S)

    Since setSince set--intersection can be written inintersection can be written interms of setterms of set--difference, it is not adifference, it is not afundamental operation.fundamental operation.

  • 8/7/2019 concepts of Relational Algebra

    16/21

    NaturalNatural--JoinJoin

    Denoted by |x|.Denoted by |x|.

    Binary operationBinary operation

    Creates a CartesianCreates a Cartesian--product of theproduct of thearguments then performs selection toarguments then performs selection toforce equality on attributes that appear inforce equality on attributes that appear in

    both relationsboth relations

  • 8/7/2019 concepts of Relational Algebra

    17/21

    DivisionDivision

    Denoted byDenoted by zz

    Binary OperationBinary Operation

    Used in queries that include the phraseUsed in queries that include the phrasefor all.for all.

  • 8/7/2019 concepts of Relational Algebra

    18/21

    Division (Contd)Division (Contd)

    Division is an operation on schema RDivision is an operation on schema R SS

    A tuple t is in rA tuple t is in r zz s if and only if:s if and only if:

    t is int is in RR SS(r) and(r) and

    For every tuple tFor every tuple tss in s, there is a tuple tin s, there is a tuple trr ininr satisfying both of the following:r satisfying both of the following:

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

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

  • 8/7/2019 concepts of Relational Algebra

    19/21

    AssignmentAssignment

    Denoted by theDenoted by the nn symbolsymbol

    Works like assignment operations inWorks like assignment operations inprogramming languagesprogramming languages

    Assignment must always be made to aAssignment must always be made to atemporary relationtemporary relation

    Does not result in display to the userDoes not result in display to the user

  • 8/7/2019 concepts of Relational Algebra

    20/21

    SummarySummary

    Relational Algebra defines a set ofRelational Algebra defines a set ofoperations that operate on relations andoperations that operate on relations and

    result in new tables.result in new tables.

    Operations can be composedOperations can be composed

    Additional operations simplify the algebraAdditional operations simplify the algebra

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

  • 8/7/2019 concepts of Relational Algebra

    21/21

    The EndThe End