lt12-relalg

download lt12-relalg

of 17

Transcript of lt12-relalg

  • 7/29/2019 lt12-relalg

    1/17

  • 7/29/2019 lt12-relalg

    2/17

    Database Systems &Applications

    Lec 12Relational Algebra

  • 7/29/2019 lt12-relalg

    3/17

    3

    Building Complex Expressions

    Combine operators with parentheses andprecedence rules.

    Three notations, just as in arithmetic:

    1. Sequences of assignment statements.

    2. Expressions with several operators.

    3. Expression trees.

  • 7/29/2019 lt12-relalg

    4/17

    4

    Sequences of Assignments

    Create temporary relation names.

    Renaming can be implied by giving relations a listof attributes.

    Example: R3 := R1 C R2 can be written:

    R4 := R1 X R2

    R3 := C(R4)

  • 7/29/2019 lt12-relalg

    5/17

    5

    Expressions in a Single Assignment

    Example: the theta-join R3 := R1C

    R2 can

    written: R3 := C

    (R1 X R2)

    Precedence of relational operators:1. [SELECT, PROJECT, RENAME] (highest).

    2. [PRODUCT, JOIN].

    3. INTERSECTION.

    4. [UNION, DIFFERENCE]

  • 7/29/2019 lt12-relalg

    6/17

    6

    Expression Trees

    Leaves are operands --- either variables standingfor relations or particular constant relations.

    Interior nodes are operators, applied to their childor children.

  • 7/29/2019 lt12-relalg

    7/17

    7

    Example

    students_t(st_id, name)

    st_courses_t(st_id1, c_no, sem, year, grade)

    Find the st_ids of all the students those are eitherA7 or grade = B.

  • 7/29/2019 lt12-relalg

    8/17

    8

    As a Tree:

    students_tst_courses_t

    SELECTdiscipline = A7 SELECTgrade = B

    PROJECTst_id

    RENAMER(st_id)

    PROJECTst_id1

    UNION

  • 7/29/2019 lt12-relalg

    9/17

    9

    Example

    st_courses_t(st_id1, c_no, sem, year, grade)

    Find the students who got same grade in two

    different courses.

    Strategy: by renaming, define a copy ofst_courses_t, called stc(st_id1, c_no1, sem, year,

    grade). The natural join ofst_courses_t and stcconsists of (st_id1, c_no, c_no1, sem,year,grade) such that the students who gotsame grade in two different courses.

  • 7/29/2019 lt12-relalg

    10/17

    10

    The Tree

    st_courses_tst_courses_t

    RENAMEstc(st_id1, c_no1, sem, year, grade)

    JOIN

    PROJECTst_id1

    SELECTc_no != c_no1

  • 7/29/2019 lt12-relalg

    11/17

    11

    Schemas for Results ---(1)

    Union, intersection, and difference: theschemas of the two operands must be thesame, so use that schema for the result.

    Selection: schema of the result is the same asthe schema of the operand.

    Projection: list of attributes tells us theschema.

  • 7/29/2019 lt12-relalg

    12/17

    12

    Schemas for Results --- (2)

    Product: schema is the attributes of bothrelations.

    Use R.A, etc., to distinguish two attributesnamedA.

    Theta-join: same as product.

    Natural join: union of the attributes of the tworelations.

    Renaming: the operator tells the schema.

  • 7/29/2019 lt12-relalg

    13/17

    Consider the following schema.

    People(PID,name,address,gender,age)

    Planet(Plname,colour,shade,Galaxy)

    Live(PID,Plname,migratedfrom,status)

    (A person can be migrated from one planet to anotherplanet at any time. Depending on that the status will be

    living or shifted. By birth the value of migratedfrom for

    a given PID is NULL)

    Property(PrID,PID,Plname,type)

    GuestHouse(GID,No.of.Bed rooms)

    Haveasite(SID,sqfts)

    Another Example

  • 7/29/2019 lt12-relalg

    14/17

    a. Find the names of people who live on redplanet.

    b. Find the names of people who live on blueplanet at present but never lived on greenplanet

    c. Find the plnames in the order of demand.(The demand is more for dark shadeplanets and then for the planets which are inMilky Way Galaxy and then for others.

  • 7/29/2019 lt12-relalg

    15/17

  • 7/29/2019 lt12-relalg

    16/17

    1 .Draw the expression tree for the followingquery.Find the names of male people who is having a

    guest house with 3 bed rooms on green colourplanet.

  • 7/29/2019 lt12-relalg

    17/17