53
Relational Algebra

Algebra - Copy

Embed Size (px)

Citation preview

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 1/53

Relational Algebra

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 2/53

Formal Relational Query Languages Two mathematical Query Languages form the

basis for practical languages (e.g. SQL) – Relational Algebra: Operational, useful for 

representing execution plans

 – Relational Calculus: Declarative: Describe

what you want, rather than how to compute it

They are NOT programming languages – Not expected to be Turing complete

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 3/53

Operations (Operators)

Operations on a single relation – selection σ, projection π

Usual set operations (relations are sets):

 – union ∪, intersection ∩, and difference   

Operations combining two or more relations

 – Cartesian product ×, join and natural join

And a renaming operation ρ

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 4/53

Projection

Keeping vertical slices of a relationaccording to L

L is a list of attributes (i.e. a list of 

columns) of the relation R

πL(R)

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 5/53

Projection

πEmp_No, Num(Assigned_To)

πEmp_No, Num( ) =

Emp_No Dep_Date Num

1001 Nov 1 100

1001 Oct 31 100

1002 Nov 1 100

1002 Oct 31 100

1003 Oct 31 100

1003 Oct 31 337

1004 Oct 31 337

1005 Oct 31 337

1006 Nov 1 991

1006 Oct 31 337

Emp_No Num

1001 100

1002 100

1003 100

1003 337

1004 337

1005 337

1006 337

1006 991

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 6/53

Projection Is πEmp_No, Num(Assigned_To) Equivalent to

SELECT Emp_No, NumFROM Assigned_To

???

100Nov 11002

100Oct 311001

100Nov 11001NumDep_DateEmp_No

πEmp_No, Num( ) =1001002

1001001

NumEmp_No

NO. Relational algebra works with sets(i.e. No dublicates)SELECT DISTINCT Emp_No, Num

FROM Assigned_To

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 7/53

Selection

Selecting the t-uples of a relation R verifying a condition c 

σc(R)

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 8/53

Selection

σ Salary<100000(Employee)

Name Salary Emp_No

Clark 150000 1006

Gates 5000000 1005

Jones 50000 1001

Peters 45000 1002

Phillips 25000 1004

Rowe 35000 1003

Warnock 500000 1007

σSalary<100000( ) =

Name Salary Emp_NoJones 50000 1001

Peters 45000 1002

Rowe 35000 1003

Phillips 25000 1004

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 9/53

Selection

σ Salary<100000(Employee)

SELECT DISTINT *FROM Employee

WHERE Salary < 100000

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 10/53

Confusing terms

SELECT DISTINCT attr1,attr2

FROM Relation

WHERE cond

Projection πattr1,attr2(Relation)

Selection σcond(Relation)

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 11/53

Selection (θ-)Condition

Term Op Term is a condition – where Term is an attribute name

 – or Term is a constant

 – Op is one of <, >, =, ≠, etc.

(C1 ∧ C2), (C1 ∨ C2), (¬ C1) are

conditions where C1 and C2 areconditions

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 12/53

Selection

σSalary>100000 ∧¬(Name=‘Gates’)(Employee)

Name Salary Emp_No

Clark 150000 1006Gates 5000000 1005

Jones 50000 1001

Peters 45000 1002

Phillips 25000 1004

Rowe 35000 1003

Warnock 500000 1007

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 13/53

Composability

The result of an expression is a relation

πEmp_No, Num(σNum>150(Employee))

200Nov 11002

100Oct 311001

100Nov 11001

NumDep_DateEmp_No

2001002

NumEmp_No

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 14/53

Composability

πEmp_No, Num(σNum>150(Employee))

What is the equivalent SQL query?

σNum>150(πEmp_No, Num(Employee))What is the equivalent SQL query?

SELECT DISTINCT Emp_No, Num

FROM Employee

WHERE Num>150

Can I always exchange the order of σ and π ?

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 15/53

Union, Intersection, Set-differenceR1 ∪ R2 = { t | t ∈ R1 or t ∈ R2}

R1 ∩ R2 = { t | t ∈ R1 and t ∈ R2}

R1 — R2 = { t | t ∈ R1 and t ∉ R2}

The relations R1 and R2 must be union

compatible – Same number of attributes

 – Corresponding attributes have the same

type (but not necessarily the same name)

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 16/53

Set operations - Union

Plane1 ∪ Plane2

Maker Model_No

Airbus A310

Airbus A320

Airbus A330

Airbus A340

MD DC10

MD DC9

Maker Model_No

Boeing B727

Boeing B747

Boeing B757

MD DC10

MD DC9

∪ =

Maker Model_No

Airbus A310

Airbus A320

Airbus A330

Airbus A340

Boeing B727

Boeing B747

Boeing B757

MD DC10

MD DC9

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 17/53

Set operations - Union Plane1 ∪ Plane2

SELECT DISTINCT *

FROM Plane1UNION

SELECT DISTINCT *

FROM Plane2

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 18/53

Set operations - Intersection

Plane1 ∩ Plane2

Maker Model_No

Airbus A310

Airbus A320

Airbus A330

Airbus A340

MD DC10

MD DC9

Maker Model_No

Boeing B727

Boeing B747

Boeing B757

MD DC10

MD DC9

∩ =Maker Model_No

MD DC9

MD DC10

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 19/53

Set operations - Intersection

Plane1 ∩ Plane2

SELECT DISTINCT *

FROM Plane1

INTERSECT

SELECT DISTINCT *FROM Plane2

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 20/53

Set operations – Set difference

Plane1 — Plane2

Maker Model_No

Airbus A310

Airbus A320

Airbus A330

Airbus A340

MD DC10

MD DC9

Maker Model_No

Boeing B727

Boeing B747

Boeing B757

MD DC10

MD DC9

— =

Maker Model_No

Airbus A310

Airbus A320

Airbus A330

Airbus A340

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 21/53

Set operations – Set difference

Plane1 — Plane2

SELECT DISTINCT *

FROM Plane1

EXCEPT

SELECT DISTINCT *FROM Plane2

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 22/53

Are all operators essential?

R∩S = ((R∪S) − (R−S)) − (S−R)

Compute all tuples belonging to R or S

Remove the ones that

 belong only to R 

Remove the ones that

 belong only to S

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 23/53

Cartesian Product Combining two relations

R1 × R2

{a, b} x {1,3} = {(a,1), (a,3), (b,1), (b,3)}

{(a,1), (a,3)} x {(a,1), (a,3)}= {( (a,1),(a,1)), ( (a,1),(a,3)),

( (a,3),(a,1)),( (a,3),(a,3))}

= {(a,1,a,1), (a,1,a,3), (a,3,a,1),(a,3,a,3)}

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 24/53

Cartesian Product

Can_fly × Plane

Emp_No Model_No

1001 B727

1001 B747

1001 DC10

1002 A320

1002 A340

1002 B757

1002 DC9

1003 A310

1003 DC9

×

Maker Model_No

Airbus A310

Airbus A320

Airbus A330

Airbus A340

Boeing B727

Boeing B747

Boeing B757

MD DC10

MD DC9

81 t-uples!!!

A310AirbusB7271001

A320AirbusB7271001

DC9MDB7471001

…………

B727BoeingB7471001

B747BoeingB7471001

B757BoeingB7471001

DC10MDB7471001

A330AirbusB7471001

A340AirbusB7471001

A320AirbusB7471001

A310AirbusB7471001

DC9MDB7271001

DC10MDB7271001

B757BoeingB7271001

B747BoeingB7271001

B727BoeingB7271001

A340AirbusB7271001

A330AirbusB7271001

A320AirbusB7271001

A310AirbusB7271001

Model_NoMaker Model_NoEmp_No

=

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 25/53

Cartesian Product

Can_fly × Plane

SELECT DISTINCT *

FROM Can_Fly, Plane

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 26/53

Combining two relations R1 and R2 on a

condition cR1 c R2 = σc(R1 × R2)

θ-Join

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 27/53

θ-Join

Can_fly Can_fly.Model_No = Plane.Model_No PlaneEmp_No Model_No

1001 B727

1001 B747

1001 DC10

1002 A320

1002 A340

1002 B757

1002 DC9

1003 A310

1003 DC9

Maker Model_No

Airbus A310

Airbus A320

Airbus A330

Airbus A340

Boeing B727

Boeing B747

Boeing B757

MD DC10

MD DC9

=

Emp_No Can_Fly.Model_No Maker Plane.Model_No

1003 A310 Airbus A310

1002 A320 Airbus A320

1002 A340 Airbus A340

1001 B727 Boeing B727

1001 B747 Boeing B747

1002 B757 Boeing B757

1001 DC10 MD DC10

1002 DC9 MD DC9

1003 DC9 MD DC9

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 28/53

θ-Join

Can_fly Can_fly.Model_No = Plane.Model_No Plane

SELECT DISTINCT *

FROM Can_Fly, Plane

WHERE Can_Fly.Model_No = Plane.Model_No

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 29/53

θ-Join

Flight1.Dest = Flight2.Origin ∧Flight1.Arr_Time < Flight2.Dept_Time

Num Origin Dest Dep_Time Arr_Time

334 ORD MIA 12:00 14:14

335 MIA ORD 15:00 17:14

336 ORD MIA 18:00 20:14

337 MIA ORD 20:30 23:53

394 DFW MIA 19:00 21:30

395 MIA DFW 21:00 23:43

Num Origin Dest Dep_Time Arr_Time

334 ORD MIA 12:00 14:14

335 MIA ORD 15:00 17:14

336 ORD MIA 18:00 20:14

337 MIA ORD 20:30 23:53

394 DFW MIA 19:00 21:30

395 MIA DFW 21:00 23:43

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 30/53

θ-Join

Flight1.Dest = Flight2.Origin ∧Flight1.Arr_Time < Flight2.Dept_Time

Flight1

.Num

Flight1.

Origin

Flight1

.Dest

Flight1.De

p_Time

Flight1.Ar 

r_Time

Flight2_ 

1.Num

Flight2.O

rigin

Flight2.

Dest

Flight2.Dep

 _Time

Flight2.Arr_ 

Time

334 ORD MIA 12:00 14:14 335 MIA ORD 15:00 17:14

335 MIA ORD 15:00 17:14 336 ORD MIA 18:00 20:14

336 ORD MIA 18:00 20:14 337 MIA ORD 20:30 23:53

334 ORD MIA 12:00 14:14 337 MIA ORD 20:30 23:53

336 ORD MIA 18:00 20:14 395 MIA DFW 21:00 23:43

334 ORD MIA 12:00 14:14 395 MIA DFW 21:00 23:43

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 31/53

The Equi-Join

Combines two relations on a conditioncomposed only of equalities of attributes

of the first and second relation and

projects only one of the redundantattributes (since they are equal)

R1 E(A1.1=A2.1 ∧… ∧A1.n = A2.n) R2

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 32/53

The Equi-Join

Can_fly E(Can_fly.Model_No = Plane.Model_No) PlaneEmp_No Model_No

1001 B727

1001 B747

1001 DC10

1002 A320

1002 A340

1002 B757

1002 DC9

1003 A310

1003 DC9

Maker Model_No

Airbus A310

Airbus A320

Airbus A330

Airbus A340

Boeing B727

Boeing B747

Boeing B757

MD DC10

MD DC9

=

Emp_No Can_Fly.Model_No Maker Plane.Model_No

1003 A310 Airbus A310

1002 A320 Airbus A320

1002 A340 Airbus A340

1001 B727 Boeing B727

1001 B747 Boeing B747

1002 B757 Boeing B757

1001 DC10 MD DC10

1002 DC9 MD DC9

1003 DC9 MD DC9

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 33/53

The Natural Join

Combines two relations on the equalityof the attributes with the same names

and projects only one of the redundant

attributes

R1 R2n

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 34/53

The Natural Join

Can_fly n PlaneEmp_No Model_No

1001 B727

1001 B747

1001 DC10

1002 A320

1002 A340

1002 B757

1002 DC9

1003 A310

1003 DC9

Maker Model_No

Airbus A310

Airbus A320

Airbus A330

Airbus A340

Boeing B727

Boeing B747

Boeing B757

MD DC10

MD DC9

=

Emp_No Model_No Maker 

1003 A310 Airbus

1002 A320 Airbus

1002 A340 Airbus

1001 B727 Boeing

1001 B747 Boeing

1002 B757 Boeing

1001 DC10 MD

1002 DC9 MD

1003 DC9 MD

n

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 35/53

Renaming If attributes or relations have the same

name (for instance when joining arelation with itself) it may be convenient

to rename one

ρ(R’(N -> N’1, Nn -> N’n), R)

The new relation R’ has the same

instance has R, its schema has

attribute N’i instead of attribute Ni

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 36/53

Renaming

ρ(Staff (Name -> Family_Name,Salary -> Gross_salary),

Employee)

Name Salary Emp_No

Clark 150000 1006

Gates 5000000 1005

Jones 50000 1001

Peters 45000 1002

Phillips 25000 1004

Rowe 35000 1003

Warnock 500000 1007

Family_ 

Name

Gross_ 

SalaryEmp_No

Clark 150000 1006

Gates 5000000 1005

Jones 50000 1001

Peters 45000 1002

Phillips 25000 1004

Rowe 35000 1003

Warnock 500000 1007

Employee Staff  

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 37/53

Renaming ρ(Staff (Name -> Family_Name,

Salary -> Gross_salary),Employee)

SELECT Name AS Family_Name,

Salary AS Gross_salary

FROM Employee Staff 

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 38/53

Complex Expression

R1 ∪ (R2 ∩ πb(R3 × ρ(R4(a -> b), R5)))

R5

R1

R2

ρ (R4(a -> b))

×

R3

πb

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 39/53

Example

Find for each employee, its name andthe model number of engine it can fly πName, Model_No (σEmployee.Emp_No=Can_fly.Emp_No(Employee ×

Can_fly))

Employee Can_fly

σEmployee.Emp_No=Can_fly.Emp_No

πName, Model_No

×

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 40/53

Example

Emp_No Model_No

1001 B727

1001 B747

1001 DC10

1002 A320

1002 A340

1002 B757

1002 DC91003 A310

1003 DC9

Name Salary Emp_No

Clark 150000 1006

Gates 5000000 1005

Jones 50000 1001

Peters 45000 1002

Phillips 25000 1004

Rowe 35000 1003

Warnock 500000 1007

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 41/53

Simple SQL and Algebra queries

Also called Project Select Join queries(PSJ)

SELECT DISTINCT student.name,course.name

FROMstudent, course

 WHERE student.take = course.id AND

student.year = 1997 

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 42/53

Simple SQL and Algebra queries

πstudent.name, course.name

(σstudent.take = course.id ∧ student.year = 1997

(student

×course))

π student.name, course.name(σ student.take = course.id (σyear =

1997(student)×course)) π student.name, course.name(σ student.year = 1997(student ⊗student.take = course.id

course))

π student.name, course.name(σ year = 1997(student) ⊗ student.take = course.id

course)

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 43/53

Example 1 Find the Employment numbers of the pilots

who can fly Airbus planes

Emp_No Model_No

1001 B727

1001 B747

1001 DC101002 A320

1002 A340

1002 B757

1002 DC9

1003 A310

1003 DC9

Maker Model_No

Airbus A310

Airbus A320

Airbus A330Airbus A340

Boeing B727

Boeing B747

Boeing B757

MD DC10MD DC9

1003

1002

Emp_No

πEmp_No(σMaker=‘Airbus’(Can_Fly⊗Plane))

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 44/53

Example 2 Find the Employment numbers of the pilots

who can fly Boeing or MD planes

Emp_No Model_No

1001 B727

1001 B747

1001 DC101002 A320

1002 A340

1002 B757

1002 DC9

1003 A310

1003 DC9

Maker Model_No

Airbus A310

Airbus A320

Airbus A330Airbus A340

Boeing B727

Boeing B747

Boeing B757

MD DC10MD DC9 1003

1002

1001

Emp_No

πEmp_No(σMaker=‘Boeing’ ∨ Maker=‘MD’

(Can_Fly⊗Plane))

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 45/53

Example 3 Find the Employment numbers of the pilots

who can fly Boeing and MD planes

Emp_No Model_No

1001 B727

1001 B747

1001 DC10

1002 A320

1002 A340

1002 B757

1002 DC9

1003 A310

1003 DC9

1002

1001

Emp_NoπEmp_No(σMaker=‘Boeing’ ∧ Maker=‘MD’

(Can_Fly⊗Plane))

WRONG! The correct is:

πEmp_No(σMaker=‘Boeing’(Can_Fly⊗Plane))∪

πEmp_No(σMaker=‘MD’(Can_Fly⊗Plane))

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 46/53

Example 4 Find the Employment numbers of the pilots

who can fly at least two Boeing planes

Emp_No Model_No

1001 B727

1001 B747

1001 DC10

1002 A320

1002 A340

1002 B757

1002 DC9

1003 A310

1003 DC9

1001

Emp_No

ρ(BP1, σMaker=‘Boeing’(Can_Fly⊗Plane))

ρ(BP2, BP1)

πBP1.Emp_No(σBP1.Model_No≠BP2.Model.No(BP1×BP2))

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 47/53

Example 4 – cont. Find the Employment numbers of the pilots

who can fly at least two Boeing planes

Emp_No Model_No

1001 B727

1001 B747

1001 DC10

1002 A320

1002 A340

1002 B757

1002 DC9

1003 A310

1003 DC9

1001

Emp_No

SELECT Can_Fly.Emp_No

FROM Can_Fly, PlaneWHERE Plane.Maker = ‘Boeing’

GROUP BY Can_Fly.Emp_No

HAVING COUNT(*) >= 2

Algebra does not support aggregations

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 48/53

Example 5 - Division

Find the Employment numbers of the pilots

who can fly all MD planes

1003

Emp_No

Emp_No Model_No

1001 B727

1001 B747

1001 DC10

1002 A320

1002 A340

1002 B757

1002 DC9

1003 A310

1003 DC9

Maker Model_No

Airbus A310

Airbus A320

Airbus A330

Airbus A340

Boeing B727

Boeing B747

Boeing B757

MD DC10

MD DC9

DC101003

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 49/53

Example 5 - Division Let A have two fields x and y

Let B have one field y

A/B contains all x tuples, such that for every y tuplein B there is a xy tuple in A

p4s4

p2s4

p2s3

p2s2

p1s2

p4s1

p3s1

p2s1

p1s1

PnoSno

A

s4

s3

s2

s1

Sno

p2

Pno

B

A/B

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 50/53

Example 5 - Division Let A have two fields x and y

Let B have one field y

A/B contains all x tuples, such that for every y tuplein B there is a xy tuple in A

p4s4

p2s4

p2s3

p2s2

p1s2

p4s1

p3s1

p2s1

p1s1

PnoSno

A

s4

s1

Sno

p4

p2

Pno

B

A/B

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 51/53

Example 5 - Division Let A have two fields x and y

Let B have one field y

A/B contains all x tuples, such that for every y tuplein B there is a xy tuple in A

p4s4

p2s4

p2s3

p2s2

p1s2

p4s1

p3s1

p2s1

p1s1

PnoSno

A

s1

Sno

p1

p4

p2

Pno

B

A/B

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 52/53

Example 5 - Division Compute all possible combinations of the first

column of A and B.

Then remove those rows that do not belong to A

Keep only the first column of the result. These are

the disqualified valuesπx( (πx(A)×B) − A)

A/B is the first column of A except of the

disqualified values

A/B = πx(A) − πx((πx(A)×B)− A)

8/6/2019 Algebra - Copy

http://slidepdf.com/reader/full/algebra-copy 53/53

Example 5 - Division Find the Employment numbers of the pilots

who can fly all MD planes

ρ(B, πModel_No(σMaker=‘MD’(Plane)))

ρ(A, Can_Fly)

πEmp_No(A) − πEmp_No((πEmp_No(A)×B)− A)

What is the corresponding SQL?