lt14-relalg

11
Database Systems &Applications Lec14 Extended Operators of Relational Algebra 

Transcript of lt14-relalg

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 1/11

Database Systems &Applications

Lec14

Extended Operators of Relational Algebra 

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 2/11

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 a given PID is NULL)

Property(PrID,PID,Plname,type)

GuestHouse(GID,No.of.Bed rooms)

Haveasite(SID

,sqfts)

Example revisited....

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 3/11

Find the names of all people who is having property onall the planets.

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 4/11

4

Division

• Goal: Produce the tuples in one relation, r, thatmatch all tuples in another relation, s

– r r (A1 , …An , B1 , …Bm )

– ss (B1 …Bm )

– r r/ ss, with attributes A1 , …An, is the set of all tuples

<a> such that for every tuple <b> in ss, <a,b> is in r r 

• Can be expressed in terms of projection, setdifference, and cross-product

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 5/11

Division (cont’d)

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 6/11

6

Schema for Student Registration System

StudentStudent (Id, Name, Addr, Status)

ProfessorProfessor (Id, Name, DeptId )

CourseCourse (DeptId, CrsCode, CrsName, Descr )TranscriptTranscript (StudId, CrsCode, Semester, Grade)

TeachingTeaching (ProfId, CrsCode, Semester )

DepartmentDepartment (DeptId, Name)

Division - Example

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 7/11

7

Division - Example

• List the Ids of students who have passed

all courses that were taught in spring 2000• Numerator :

– StudId and CrsCode for every course passedby every student:

π StudId, CrsCode (σ Grade≠ ‘F’  (TranscriptTranscript) )

• Denominator :–   CrsCode of all courses taught in spring 2000

π

CrsCode (σ

Semester=‘S2000’ (TeachingTeaching) )

• Result is numerator/denominator 

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 8/11

  Division Example2

sno pno

s1 p1

s1 p2

s1 p3s1 p4

s2 p1

s2 p2

s3 p2

s4 p2

s4 p4

pno

p2pnop2

p4

pnop1

p2

p4

sno

s1

s2s3

s4

snos1

s4snos1

 A

B1

B2B3

 A/B1  A/B2 A/B3

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 9/11

Expressing A/B Using Basic Operators

Division is not essential op; just a useful shorthand. (Also true of joins, but joins are so common that systems

implement joins specially.)

• Idea: For A/B, compute all x values that are not

`disqualified’ by some y value in B.  x value is disqualified if by attaching y value from B, we

obtain an xy tuple that is not in A.

Disqualified  x  values: π π   x x A B A(( ( ) ) )× −

  A/B: π   x A( ) − Disqualified x values

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 10/11

Example

s

Reserves

Sailors

Boats

sid bid day

22 101 10/10/96

58 103 11/12/96sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.558 rusty 10 35.0

bid bname color101 Interlake Blue

102 Interlake Red

103 Clipper Green

104 Marine Red

7/29/2019 lt14-relalg

http://slidepdf.com/reader/full/lt14-relalg 11/11

Find the names of sailors who’ve reserved all boats

Uses division; schemas of the input

relations to / must be carefully chosen:

 ρ π π ( , ( , Re ) / ( ))Tempsids sid bid serves bid Boats

π   sname Tempsids Sailors( )