Docsity

Relational Algebra - Data Base Management System - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

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

Relational Algebra and My

SQL

Functional Dependency

  • For all t1, t2 element of R if t1[A] = t2[A] => t1[B] = t2[B]

A, B ⊆ R(….) , we say FD: A→B if

Definition: A⊆ R(….) is a key if FD: A →R

Definition: A⊆ R(….) is a candidate key if FD: A→R

and not proper subset of A is a key.

Queries:

  • Q1: AR (…) A is not a key
  • Q2: BR (…) B is not a key
  • Q3: CR (…) C is not a key
  • Q4: DR (…) Yes-D is a key
  • Q5: ABR (…) AB together cannot form the key
  • Q6: ACR (…) Yes- AC together cannot form the key
  • Q7: BCR (…) BC together cannot form the key

Relational Algebra

  • Basic operations:
    • Selection ( ) Selects a subset of rows from relation.
    • Projection ( ) Deletes unwanted columns from relation.
    • Cross-product ( ) Allows us to combine two relations.
    • Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
    • Union ( ) Tuples in reln. 1 and in reln. 2.
  • Additional operations:
    • Intersection, join , division, renaming: Not essential, but (very!) useful.
  • Since each operation returns a relation, operations can be composed! (Algebra is “closed”.) σ π − × 

Selection

  • Selects rows that satisfy selection condition.
  • Schema of result identical to schema of (only) input relation.
  • Result relation can be the input for another relational algebra operation! ( Operator composition. ) σ rating S

8 ( 2 ) sid sname rating age

28 yuppy 9 35. 58 rusty 10 35. sname rating yuppy 9 rusty 10 π σ sname rating (^) rating S , ( ( ))

8 2

Union, Intersection, Set-Difference

  • All of these operations take two input relations, which must be union-compatible : - Same number of fields. - `Corresponding’ fields have the same type.
  • What is the schema of result? sid sname rating age 22 dustin 7 45. 31 lubber 8 55. 58 rusty 10 35. 44 guppy 5 35. 28 yuppy 9 35. sid sname rating age 31 lubber 8 55. 58 rusty 10 35. S 1 ∪ S 2 S 1 ∩ S 2 sid sname rating age 22 dustin 7 45. S 1 − S 2

Joins

  • Condition Join :
  • Result schema same as that of cross-product.
  • Fewer tuples than cross-product. Filters tuples not satisfying the join condition.
  • Sometimes called a theta-join. R c S c  = σ ( R × S ) (sid) sname rating age (sid) bid day 22 dustin 7 45.0 58 103 11/12/ 31 lubber 8 55.5 58 103 11/12/ S R S sid R sid 1 1 1 1  . <.

Joins

  • Equi-Join : A special case of condition join where the condition c contains only equalities****.
  • Result schema similar to cross-product, but only one copy of fields for which equality is specified.
  • Natural Join : Equijoin on all common fields. sid sname rating age bid day 22 dustin 7 45.0 101 10/10/ 58 rusty 10 35.0 103 11/12/

,.., , ,.. S R sid agebid sid π 

Examples of Division A/B

sno pno s1 p s1 p s1 p s1 p s2 p s2 p s3 p s4 p s4 p pno p pno p p pno p p p sno s s s s sno s s sno s A B B B A/B1 A/B2 A/B

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. Division is NOT implemented in SQL).
  • Idea : For SALES/PRODUCTS , compute all products such that there exists at least one supplier not supplying it. - x value is disqualified if by attaching y value from B , we obtain an xy tuple that is not in A. (( ( Sales ) Pr oducts ) Sales ) sid sid A =π π × −

The answer is π

sid (Sales) - A

EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN

  • Equality join connects tuples from two relations that match on certain attributes.

The specified joining columns are kept in the resulting relation.

  • ∏name(бdname=‘toy’(Emp Dept)))
  • Natural join connects tuples from two relations that match on the specified

common attributes

  • ∏name(бdname=‘toy’(Emp Dept)))
  • How is an equality join between Emp and Dept using dno different than a natural

join between Emp and Dept using dno?

  • Equality join: SS#, name, age, salary, Emp.dno, Dept.dno, …
  • Natural join: SS#, name, age, salary, dno, dname, …
  • Join is similar to equality join using different comparison operators
  • A S op = {=, ≠, ≤, ≥, <, >} att op att (dno) (dno)

EXAMPLE JOIN

  • Natural Join, (Emp Dept))) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 1 3 Bob 22 27000 1 4 Kathy 30 30000 2 5 Shideh 4 4000 1

EMP

dno dname floor mgrss# 1 Toy 1 5 2 Shoe 2 1

Dept

(dno) SS# Name Age Salary dno dname floor mgrss# 1 Joe 24 20000 2 Shoe 2 1 2 Mary 20 25000 1 Toy 1 5 3 Bob 22 27000 1 Toy 1 5 4 Kathy 30 30000 2 Shoe 2 1 5 Shideh 4 4000 1 Toy 1 5

EXAMPLE JOIN

  • Join, (Emp ρ x (Emp)))) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 1 3 Bob 22 27000 1 4 Kathy 30 30000 2 5 Shideh 4 4000 1

EMP

dno dname floor mgrss#

1 Toy 1 5

2 Shoe 2 1

Dept

Salary > 5 * salary SS# Name Age Salary dno x.SS# x.Name x.Age x.Salary x.dno 2 Mary 20 25000 1 2 Shideh 4 4000 1 3 Bob 22 27000 1 3 Shideh 4 4000 1 4 Kathy 30 30000 2 4 Shideh 4 4000 1