07.05 division

13
DBMS Relational Algebra Division BISHAL GHIMIRE [email protected]

Upload: bishal-ghimire

Post on 30-May-2015

44 views

Category:

Education


2 download

TRANSCRIPT

Page 1: 07.05 division

DBMS

Relational Algebra

Division

BISHAL [email protected]

Page 2: 07.05 division

DivisionDivision is not essential operation ; just a useful shorthand.

Also true of joins, but joins are so common that systems implement joins specially. Division is NOT implemented in SQL.

Not supported as a primitive operator, but useful for many expressing queries

Precondition: in A/B, the attributes in B must be included in the schema for A. Also, the result has attributes A-B.SALES(supId, prodId); PRODUCTS(prodId); Relations SALES and PRODUCTS must be built using

projections. SALES/PRODUCTS: the ids of the suppliers supplying ALL

products

Page 3: 07.05 division

The division is a binary operation that is written as R ÷ S

The result consists of the restrictions of tuples in R to the attribute names unique to R

The header of R but not in the header of S, for which it holds that all their combinations with tuples in S are present in R

Page 4: 07.05 division

Student Task

Fred Database1

Fred Database2

Fred Compiler1

Eugene Database1

Eugene Compiler1

Sarah Database1

Sarah Database2

Completed DBProject

Task

Database1

Database2

Completed÷

DBProject

Student

Fred

Sarah

Page 5: 07.05 division

sNo pNo

s1 p1

s1 p2

s1 p3

s1 p4

s2 p1

s2 p2

s3 p2

s4 p2

s4 p4

pNo

p2

A B1 A/B1sNo

s1

s2

s3

s4

Page 6: 07.05 division

sNo pNo

s1 p1

s1 p2

s1 p3

s1 p4

s2 p1

s2 p2

s3 p2

s4 p2

s4 p4

pNo

p2

p4

A B2 A/B2sNo

s1

s4

Page 7: 07.05 division

sNo pNo

s1 p1

s1 p2

s1 p3

s1 p4

s2 p1

s2 p2

s3 p2

s4 p2

s4 p4

pNo

p1

p2

p4

A B3 A/B3sNo

s1

Page 8: 07.05 division

Suppose that employees of the company are assigned to some professional courses; each employee follows several courses and each course is taken by several employees

Information about courses and which employee takes which course can be represented by the following relationsTakes (Emp#, Course#)Courses (Course#, Topic, Weeks)

Page 9: 07.05 division

Assume that these two relations have the following content

Emp# Course#

7369 10

7369 20

7369 30

7782 10

7782 40

Course# Topics Weeks

20 CAD 5

10 DB 3

30 DB 2

40 OS 4

Takes Courses

Page 10: 07.05 division

Consider the following query: “determine the emp# of the employees taking all the courses with topic DB” (Q1)

The course# of the courses relevant to the query is obtained through the following expression:R1 = ∏ Course# (σ Topic=“DB”) (Courses))

The result of such query is {10, 30}

Page 11: 07.05 division

The result of query Q1 is represented by all the employees that appear in the Takes relation with each of the course numbers retrieved by query R1

The correct result of Q1 is thus {7369}The operation that allows one to execute

such query is the division

Page 12: 07.05 division

Let R and S be relations; let UR and US be the attribute

sets of R and S, respectively, and such that UR US

The division operation is denoted asR ÷ S

and is expressed as follows∏(UR -US) (R) - ∏ (UR -US) ((∏ (UR –US) (R) X S) – R)The expression on the right side of the – sign

determines all tuples of R that are not associated with at least a tuple of S

Page 13: 07.05 division

Query Q1 is thus expressed as follows:Takes ÷ ∏ Course# (σ Topic=“DB”) (Courses))

R = TakesS = ∏ Course# (σ Topic=“DB”) (Courses))

S = {10, 30}UR = {Emp#, Course#}

Us = {Course#}