Академический Документы
Профессиональный Документы
Культура Документы
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:
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
11
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.