Docsity

Relational Algebra Examples (1), Slides of Algebra

Toggle sidebar
bg1
1
Relational Algebra 1
Relational Algebra
Operators
•Set operators
– Union
– Intersection
– Difference
– Cartesian product
•Relational operators
– Selection
– Projection
– Join
– Division
Relational Algebra 2
a1
a2
a3
a4
a1
a2
a3
a4
b1
b1
b2
b2
b1
b2
b2
b3
c1
c2
c3
c3
a1
a2
a3
a3
a4
a4
b1
b1
b2
b2
b2
b2
c1
c1
c2
c3
c2
c3
(natural)
join (*)
Special Relational Operators in Pictures
select (σ)
project (π)
eliminate
duplicates
A B B C A B C
a
a
a
a
b
c
x
y
z
z
x
y
x
za
Divide
(÷)
D E
ED
Relational Algebra 3
Examples (1): Select (σ) and Project (π)
EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)
DEPT( DNO, DNAME, LOC)
Q1. Find the subset of employees who are in dept. 50.
σDNO =50 (EMP)
Q2. Find the subset of employees who are in dept. 25, 47 or 53.
σDNO =25 or DNO =47 or DNO =53 (EMP)
Q3. List names and jobs of all employees.
πNAME, JOB (EMP)
Relational Algebra 4
Examples (2):
More Complex Select (σ) and Project (π)
EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)
DEPT( DNO, DNAME, LOC)
Q4. List names and jobs of employees in departments
25, 47 or 53.
πNAME, JOB (σDNO =25 or DNO =47 or DNO =53 (EMP))
D_EMPS ← σDNO =25 or DNO =47 orDNO =53 (EMP)
RESULT ← π NAME, JOB (D_EMP)
Relational Algebra 5
Examples (3): Union (∪) and Difference (-)
EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)
DEPT( DNO, DNAME, LOC)
Q5. List the numbers of those departments which
have an employee named ‘Smith’ or are located
in ‘Columbus’.
πDNO (σNAME = ‘Smith’ (EMP))
∪
πDNO (σLOC = ‘Columbus’ (DEPT))
Q6. List the DNO for departments that have no employees.
πDNO (DEPT) -πDNO (EMP)
Relational Algebra 6
Theta-join (
θ
-Join)
θ ε
{≥, <, =, >, ≤, ≠}
Examples (4): Join: Natural (*) and Theta ( )
EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)
DEPT( DNO, DNAME, LOC)
✕
Q7. List the names of all employees and the locations of their
departments.
✕
πNAME, LOC (EMP EMP.DNO =DEPT.DNO (DEPT) )✕
πNAME, LOC (EMP * DEPT) ) * Natural
join
pf3
pf4
pf5

Partial preview of the text

Download Relational Algebra Examples (1) and more Slides Algebra in PDF only on Docsity!

Relational Algebra 1

Relational Algebra

Operators

• Set operators

  • Union
  • Intersection
  • Difference
  • Cartesian product

• Relational operators

  • Selection
  • Projection
  • Join
  • Division

Relational Algebra 2

a a a a

a a a a

b b b b

b b b b

c c c c

a a a a a a

b b b b b b

c c c c c c

(natural) join (*)

Special Relational Operators in Pictures

select (σ)

project (π)

eliminate duplicates

A B B C

A B C

a a a a b c x y z z x y x z

a

Divide (÷)

D E

E D

Relational Algebra 3

Examples (1): Select (σ) and Project (π)

EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)

DEPT( DNO, DNAME, LOC)

Q1. Find the subset of employees who are in dept. 50. σDNO = 50 (EMP)

Q2. Find the subset of employees who are in dept. 25, 47 or 53. σDNO = 25 or DNO = 47 or DNO = 53 (EMP)

Q3. List names and jobs of all employees. π (^) NAME, JOB (EMP)

Relational Algebra 4

Examples (2):

More Complex Select (σ) and Project (π)

EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)

DEPT( DNO, DNAME, LOC)

Q4. List names and jobs of employees in departments 25, 47 or 53.

π (^) NAME, JOB (σDNO = 25 or DNO = 47 or DNO = 53 (EMP))

D_EMPS ← σDNO = 25 or DNO = 47 or DNO = 53 (EMP) RESULT ← π (^) NAME, JOB (D_EMP)

Relational Algebra 5

Examples (3): Union (∪) and Difference (-)

EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)

DEPT( DNO, DNAME, LOC)

Q5. List the numbers of those departments which have an employee named ‘Smith’ or are located in ‘Columbus’. π (^) DNO (σ (^) NAME = ‘Smith’ (EMP)) ∪ π (^) DNO (σ (^) LOC = ‘Columbus’ (DEPT))

Q6. List the DNO for departments that have no employees. π (^) DNO (DEPT) - π (^) DNO (EMP) Relational Algebra 6

Theta-join ( θ -Join)

Examples (4): Join: Natural (*) and Theta ( )

EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)

DEPT( DNO, DNAME, LOC)

Q7. List the names of all employees and the locations of their departments.

^5 

π (^) NAME, LOC (EMP ^5  EMP.DNO = DEPT.DNO (DEPT) )

π (^) NAME, LOC (EMP * DEPT) )

* Natural join

Relational Algebra 7

Natural join ()* Q ← R 1 * (^) (list1), (list2) R 2 listi has attributes from relation Ri list 1 names go in Q

Theta ( θ ) join ( )

Q ← R 1 R 2 where < join condition > is of the form AND AND … AND where each condition is of the form Ai θ Bj, Ai is an attribute of R 1 , Bj is an attribute of R 2 , θ (theta) ε (<, ≤, >, ≥, =, ≠) e.g. For each employee, those making higher salary. EMP1 (^)  (^5) EMP1.SAL < EMP2.SAL EMP

Relational Algebra 8

Examples (5): Select, project and join

EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)

DEPT( DNO, DNAME, LOC)

Q8. Find the names of employees who work in Ann Arbor.

π (^) NAME (EMP (^) EMP.DNO = DEPT.DNO (σ (^) LOC = ‘Ann Arbor’(DEPT) )

π (^) NAME (EMP * (σ (^) LOC = ‘Ann Arbor’(DEPT) ) Natural join *

equi-join

when θ is ‘=‘

Relational Algebra 9

Examples (6): Not Equal (≠) in Select

EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)

DEPT( DNO, DNAME, LOC)

Q9. Find the names of employees who do not work in Ann Arbor.

π (^) NAME (EMP (^) EMP.DNO = DEPT.DNO ^5  (σ (^) LOC ≠ ‘Ann Arbor’(DEPT) )

^5 

π (^) NAME (EMP * (σ (^) LOC ≠ ‘Ann Arbor’(DEPT) ) Natural join *

( θ ) Join

Relational Algebra 10

X ← EMP Y ← EMP

π (^) X.NAME, Y.NAME (σ (^) X.SAL > Y.SAL (X (^) Y.EMPNO = X.MGR Y))

Examples (7): A “classic”,

equi-join as product ( 5 ) and select (σ)

EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)

EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)

 5 

Q10. For each employee whose salary exceeds his or her manager’s salary, list the employee’s name and the manager’s name.

π (^) X.NAME, Y.NAME (σ (^) X.SAL > Y.SAL and Y.EMPNO = X.MGR (X 5 Y))

Relational Algebra 11

Examples (8): A new kind of query

EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)

DEPT( DNO, DNAME, LOC)

Q11. Find the numbers of those departments that have employees who can do some job that is done by an employee in department D3.

Q12. Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3.

Relational Algebra 12

Examples (8): continued

EMP( EMPNO, … , DNO, JOB,.. .)

100 D3 electrician 200 D3 plumber 300 D3 electrician 400 D1 electrician 500 D1 plumber 600 D1 carpenter 700 D2 electrician 800 D2 carpenter 900 D2 electrician

Q11. Find the numbers of those departments that have employees who can do some job that is done by an employee in department D3. Answer: D1 and D

Relational Algebra 19

Q13. Find the average salary of all employees.

AVERAGE SAL ( EMP)

Examples (10): Functions and groups

EMP( EMPNO, DNO, SAL,...)

100 D3 66,

200 D3 55,

300 D3 66,

400 D1 65,

500 D1 55,

600 D1 60,

700 D2 66,

800 D2 60,

900 D2 66,

Relational Algebra 20

Examples (10): Functions and groups continued

Q14. List the departments (DNO) and the average salary of each.

DNO

AVERAGE SAL ( EMP)

EMP( EMPNO, DNO, SAL,...)

100 D3 66,

200 D3 55,

300 D3 66,

400 D1 66,

500 D1 55,

600 D1 60,

700 D2 66,

800 D2 60,

900 D2 66,

DNO AVG SAL

D3 62,

D1 60,

D2 64,

Relational Algebra 21

Examples (11): Outer joins

S(S#, SNAME, CITY) S1 Smith London S5 Adams Athens

P(P#, PNAME, CITY)

P1 Nut London P3 Screw Rome

Q15a. For each part list all suppliers in the same city.

Q15b. For each part list all suppliers in the same city. For a part with no supplier in the city, list null.

( P#, PNAME, S#, SNAME, CITY)

P1 Nut S1 Smith London

P  5 P.CITY = S.CITY S

P  5 P.CITY = S.CITY S

natural join (inner join)

( P#, PNAME, S#, SNAME, CITY)

P1 Nut S1 Smith London P3 Screw?? Rome left outer join Relational Algebra 22

Examples (11): Outer joins continued

Q15c. For each part list all suppliers in the same city. For a supplier with no part in the city, list null.

Q15d. For each part list all suppliers in the same city. For a part with no supplier in the city, list null. For a supplier with no part in the city, list null.

( P#, PNAME, S#, SNAME, CITY) P1 Nut S1 Smith London ?? S5 Adams Athens

P P.CITY = S.CITY S

right outer join

( P#, PNAME, S#, SNAME, CITY) P1 Nut S1 Smith London P3 Screw?? Rome ?? S5 Adams Athens outer join

^5 

P 5  P.CITY = S.CITY S

S(S#, SNAME, CITY)

S1 Smith London S5 Adams Athens

P(P#, PNAME, CITY)

P1 Nut London P3 Screw Rome

Relational Algebra 23

Examples (12): Recursive closure

Q 16. List all the superiors of EMPNO 500. 600, 950, 980

Q 17. List all those supervised by EMPNO 900. 700, 800, 100, 200, 300, 400

Can’t express these queries? WHY?

980

900 950

700 800 600 850

100 200 300 400 500

Relational Algebra 24

Examples (12): Recursive closure

Q 16. List all the superiors of EMPNO 500. 600, 950, 980

Q 17. List all those supervised by EMPNO 900. 700, 800, 100, 200, 300, 400

Can’t express these queries? WHY?

EMP( EMPNO, MGR,...) 100 700 200 700 300 800 400 800 500 600 600 950 700 900 800 900 850 950 900 980 950 980 980 980

Relational Algebra 25

Q 16. List all the superiors of EMPNO 500. SUPERIORS (EMPNO, MGR) 500 600 200 700 … 950 980 980 980 500 950 200 900 300 900 400 900 500 980 600 980 700 980 800 980 500 980 200 980 300 980 400 980

Initial table, EMP table.

Second addition. Join & Union.

First addition. Join & Union.

EMP( EMPNO, MGR,...) 100 700 200 700 300 800 400 800 500 600 600 950 700 900 800 900 850 950 900 980 950 980 980 980

Can’t express these queries? WHY? Relational Algebra 26

Important Concepts

• Relational completeness

– Language is sufficiently powerful.

– Is a new language this powerful?

  • Must be able to translate any relational algebra statement to the new language.

Relational Algebra 27

Important Concepts, Continued

• Fully relational database

– Relational structure

  • Domain
  • Integrity rules
    • Key
    • Entity
    • Referential

– Language as powerful as relational algebra