Docsity

Set Operators - Data Base Management System - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Set Operators - Data Base Management System - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Set Operators

• Relation is a set of tuples, so set operations

should apply: ∩, ∪, − (set difference)

• Result of combining two relations with a set

operator is a relation; hence all its elements

must be tuples having the same structure

• Hence, scope of set operations limited to

union compatible relations

Union Compatible Relations

• Two relations are union compatible if

  • Both have same number of columns
  • Names of attributes are the same in both
  • Attributes with the same name in both relations

have the same domain

• Union compatible relations can be combined

using union , intersection , and set difference

Cartesian Product

  • If R and S are two relations, R × S is the set of all concatenated tuples , where x is a tuple in R and y is a tuple in S (but see naming problem next)
  • R × S is expensive to compute:
    • Factor of two in the size of each row
    • Quadratic in the number of rows

A B C D A B C D

x1 x2 y1 y2 x1 x2 y1 y

x3 x4 y3 y4 x1 x2 y3 y

x3 x4 y1 y

R S x3 x4 y3 y

R × S Docsity.com

Renaming in Cartesian Product

Result of expression evaluation is a relation.

Attributes of relation must have distinct names.

So what do we do if they don’t?

E.g., suppose R(A,B) and S(A,C) and we wish to compute R × S. One solution is to rename the attributes of the answer:

R × S( R.A, R.B, S.A, S.C)

Although only A needs to be renamed, it is“cleaner” to rename them all.

Example

This is a relation with 4 attributes: StudId, CrsCode1, ProfId, CrsCode

Transcript ( StudId, CrsCode, Semester, Grade )

Teaching ( ProfId, CrsCode, Semester )

π StudId, CrsCode (Transcript)[ StudId, CrsCode1 ]

× π ProfId, CrsCode (Teaching) [ ProfId, CrsCode2 ]

Derived Operation: Join

A ( general or theta ) join of R and S is the expression R (^) join-condition S where join-condition is a conjunction of terms:

Ai oper Bi in which Ai is an attribute of R; Bi is an attribute of S; and oper is one of =, <, >, ≥ ≠, ≤.

The meaning is:

σ join-condition ´ ( R × S )

where join-condition and join-condition ´ are the same, except for possible renamings of attributes caused by the Cartesian product.

Relational Algebra

  • Relational algebra operations operate on relations and produce relations (“closure”) f: Relation -> Relation f: Relation x Relation -> Relation
  • Six basic operations:
    • Projection π (^) Α (R)
    • Selection σ (^) θ (R)
    • Union R 1 [ R (^2)
    • Difference R 1 – R (^2)
    • Product R 1 £ R (^2)
    • (Rename) ρ (^) Α−>Β (R)

Equijoin Join - Example

π Name,CrsCode ( Student Id=StudId σ Grade=‘A’ (Transcript))

Id Name Addr Status

111 John ….. …..

222 Mary ….. …..

333 Bill ….. …..

444 Joe ….. …..

StudId CrsCode Sem Grade

111 CSE305 S00 B

222 CSE306 S99 A

333 CSE304 F99 A

Mary CSE

Bill CSE

The equijoin is used very frequently since it combines related data in different relations.

Student (^) Transcript

Equijoin : Join condition is a conjunction of equalities.

Natural Join (cont’d)

• More generally:

R S = π attr-list ( σ join-cond ( R × S ) )

where

attr-list = attributes ( R ) ∪ attributes ( S )

(duplicates are eliminated) and join-cond has

the form:

A 1 = A 1 AND … AND An = A n

where

{ A 1 … An } = attributes ( R ) ∩ attributes ( S )

Natural Join Example

• List all Ids of students who took at least two

different courses:

π StudId ( σ CrsCode ≠ CrsCode2 (

Transcript

Transcript [ StudId, CrsCode2, Sem2, Grade2 ] ))

We don’t want to join on CrsCode , Sem , and Grade attributes, hence renaming!

Natural Join and Intersection

Natural join: special case of join where θ is

implicit – attributes with same name must be

equal:

STUDENT ⋈ Takes ´

STUDENT ⋈STUDENT.sid = Takes.sid Takes

Intersection: as with set operations,

derivable from difference

A-B B-A

A B

A ∩ B

Division

• A somewhat messy operation that can be

expressed in terms of the operations we have

already defined

• Used to express queries such as “The fid's of

faculty who have taught all subjects”

• Paraphrased: “The fid’s of professors for which

there does not exist a subject that they

haven’t taught”

Division: R 1 ÷ R 2

• Requirement: schema(R 1 ) ¾ schema(R 2 )

• Result schema: schema(R 1 ) – schema(R 2 )

• “Professors who have taught all courses”:

• What about “Courses that have been

taught by all faculty”?

πfid (πfid,subj (Teaches ⋈ COURSE) ÷ πsubj (COURSE))

Division

• Goal: Produce the tuples in one relation, r,

that match all tuples in another relation, s

  • r (A 1 , …A (^) n, B 1 , …B (^) m)
  • s (B 1 …B (^) m)
  • r/s , with attributes A 1 , …A (^) n , is the set of all

tuples such that for every tuple in s,

is in r

• Can be expressed in terms of projection, set

difference, and cross-product