Вы находитесь на странице: 1из 17

Database Systems &Applications

Lec 12 Relational Algebra

Building Complex Expressions

Combine operators with parentheses and precedence rules. Three notations, just as in arithmetic: 1. Sequences of assignment statements. 2. Expressions with several operators. 3. Expression trees.

Sequences of Assignments
Create temporary relation names. Renaming can be implied by giving relations a list of attributes. Example: R3 := R1 R4 := R1 X R2 R3 := C (R4)
C

R2 can be written:

Expressions in a Single Assignment


Example: the theta-join R3 := R1 written: R3 := C (R1 X R2) R2 can

Precedence of relational operators: 1. [SELECT, PROJECT, RENAME] (highest). 2. [PRODUCT, JOIN]. 3. INTERSECTION. 4. [UNION, DIFFERENCE]

Expression Trees
Leaves are operands --- either variables standing for relations or particular constant relations. Interior nodes are operators, applied to their child or children.

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 either A7 or grade = B.

As a Tree:
UNION RENAMER(st_id) PROJECTst_id SELECTdiscipline = A7 students_t PROJECTst_id1 SELECTgrade = B st_courses_t
8

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 of st_courses_t, called stc(st_id1, c_no1, sem, year, grade). The natural join of st_courses_t and stc consists of (st_id1, c_no, c_no1, sem, year,grade) such that the students who got same grade in two different courses.
9

The Tree
PROJECTst_id1 SELECTc_no != c_no1 JOIN

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


10

Schemas for Results ---(1)


Union, intersection, and difference: the schemas of the two operands must be the same, so use that schema for the result. Selection: schema of the result is the same as the schema of the operand. Projection: list of attributes tells us the schema.

11

Schemas for Results --- (2)


Product: schema is the attributes of both relations. Use R.A, etc., to distinguish two attributes named A. Theta-join: same as product. Natural join: union of the attributes of the two relations. Renaming: the operator tells the schema.

12

Another Example
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 another planet at any time. Depending on that the status will be living or shifted. By birth the value of migratedfrom for given PID is NULL) Property(PrID,PID,Plname,type) GuestHouse(GID,No.of.Bed rooms) Haveasite(SID,sqfts)

a. Find the names of people who live on red planet. b. Find the names of people who live on blue planet at present but never lived on green planet c. Find the plnames in the order of demand. (The demand is more for dark shade planets and then for the planets which are in Milky Way Galaxy and then for others.

1 .Draw the expression tree for the following query. Find the names of male people who is having a guest house with 3 bed rooms on green colour planet.

Вам также может понравиться