The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline...

66
The Relational Algebra Database System-dww

Transcript of The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline...

Page 1: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

The Relational Algebra

Database System-dww

Page 2: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-2

Database System-dww

Chapter Outline

Fundamental OperatorDeliverable & Additional Operator

Page 3: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-3

Database System-dww

Basic Relational Operations: Unary Operations

SELECT PROJECT or .

Binary Operations Set operations:

UNION INTERSECTION DIFFERENCE –

CARTESIAN PRODUCT JOIN operations

What is Relational Algebra?Relational Algebra is a Procedural ParadigmYou Need to Tell What/How to Construct the ResultConsists of a Set of Operators Which, When Applied to Relations, Yield Relations (Closed Algebra)

Page 4: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-4

Database System-dww

Relational Algebra

R S unionR S intersectionR \ S set differenceR S Cartesian

product

A1, A2, ..., An (R) projection

F (R) selection

R S natural join

R S theta-joinRS division[A1 B1,.., An Bn] rename

Page 5: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-5

Database System-dww

Selection

Selects the Set of Tuples (Rows) From a Relation, Which Satisfy a Selection ConditionGeneral Form selection condition> (R)

R is a Relation Selection condition is a Boolean Expression on the Attributes of RResulting Relation Has the Same Schema as R

Select Finds and Retrieves All Relevant Rows (Tuples) of Table/Relation R which Includes ALL of its Columns (Attributes)

Page 6: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-6

Database System-dww

ENO ENAME TITLE

E1 J. Doe Elect. EngE6 L. Chu Elect. Eng.

TITLE='Elect. Eng.'(EMP)ENO ENAME TITLE

E1 J. Doe Elect. Eng.

E2 M. Smith Syst. Anal.

E3 A. Lee Mech. Eng.

E4 J. Miller Programmer

E5 B. Casey Syst. Anal.

E6 L. Chu Elect. Eng.

E7 R. Davis Mech. Eng.

E8 J. Jones Syst. Anal.

EMP

TITLE='Elect. Eng.’ OR TITLE=‘Mech.Eng’(EMP)

Selection Example

Page 7: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-7

Database System-dww

Another Selection Example

ASCnullWBnullnull

Page 8: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-8

Database System-dww

A SELECT Condition is a Boolean Expression

Form F1 F2 Fq (Q>=1), Where

Fi (I=1,…,q) are Atomic Boolean Expressions of the Form

ac or ab, a, b are Attributes of R and c is a Constant.

The Operator is one of the Arithmetic Comparison Operators: <, >, =, <>The Operator is one of the Logical Operators: , , ¬Nesting: ( )

Selection Condition

Page 9: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-9

Database System-dww

Select certain Columns (Attributes) Specified in an Attribute List X From a Relation R

General Form <attribute-list>(R)

R is a RelationAttribute-list is a Subset of the Attributes of R Over Which the Projection is Performed

Project Retrieves Specified Columns of Table/Relation R which Includes ALL of its Rows (Tuples)

Projection

Page 10: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-10

Database System-dww

Projection Example

PNO,BUDGET(PROJ)

PNO BUDGET

P1 150000

P2 135000

P3 250000

P4 310000

P5 500000

PROJ

PNO BUDGET

P2 135000

P3 250000

P4 310000P5 500000

PNAME

P1 150000Instrumentation

Database Develop.

CAD/CAM

MaintenanceCAD/CAM

Page 11: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-11

Database System-dww

Other Projection Examples

Page 12: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-12

Database System-dww

Relational Algebra Expression

Several Operations can be Combined to form a Relational Algebra Expression (query)Example: Retrieve all Customers over age 60?

Method 1: CNAME, ADDRESS, AGE ( AGE>60(CUSTOMER) )

Method 2: Senior-CUST(C#, Addr, Age)

= CNAME, ADDRESS, AGE ( AGE>60(CUSTOMER) )

Method 3: CNAME, ADDRESS, AGE (C) where

C AGE>60(CUSTOMER)

Page 13: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-13

Database System-dww

ENO ENAME TITLE

E1 J. Doe Elect. Eng.

E2 M. Smith Syst. Anal.

E3 A. Lee Mech. Eng.

E4 J. Miller Programmer

E5 B. Casey Syst. Anal.

E6 L. Chu Elect. Eng.

E7 R. Davis Mech. Eng.

E8 J. Jones Syst. Anal.

EMP TITLE(PROJ)

Elect.EngSyst.AnalMech.EngProgrammer

TITLE

Characteristics of Projection

The PROJECT Operation Eliminates Duplicate Tuples in the Resulting Relation

Why?Projection Must Maintain a Mathematical Set (No Duplicate Elements)

Page 14: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-14

Database System-dww

Selection with Projection Example

Page 15: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-15

Database System-dww

Renaming

The RENAME operator gives a new schema to a relation.R1 := RENAMER1(A1,…,An)(R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2.Simplified notation: R1(A1,…,An) := R2.Other use () notation

R2 R1(A1,…,An)

Page 16: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-16

Database System-dww

Sequences of Assignments

Create temporary relation names.Renaming can be implied by giving relations a list of attributes.Example: R3 := R1 JOINC R2 can be written:R4 := R1 * R2R3 := SELECTC (R4)

ORR4 R1 * R2R3 SELECTC (R4)

Page 17: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-17

Database System-dww

General FormR S

where R, S are Relations Result contains Tuples from both R and SDuplications are RemovedThe two Operands R, S should be union-compatible

Example: “find students registered for course C1 or C3”

s#(CNO=‘C1’ (S-C))

s#(

CNO=‘C3’ (S-C))

Union

Page 18: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-18

Database System-dww

Union CompatibilityTwo Relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are said Union-compatible If and Only If They Have

The Same Number of AttributesThe Domains of Corresponding Attributes are Compatible, i.e., Dom(Ai)=dom(Bi) for i=1, 2, ..., N

Names Do Not Have to be Same!

For Relational Union and Difference Operations, the Operand Relations Must Be Union CompatibleThe Resulting Relation for Relational Set Operations

Has the Same Attribute Names as the First Operand Relation R1 (by Convention)

Page 19: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-19

Database System-dww

General Form

R – S

where R and S are RelationsResult Contains all tuples that are in R, but not in S.

R – S <> S – R

Again, there Must be Compatibility

Example “Find the students who registered course C1 but not

C3” s#(CNO=‘C1’

(S-C)) –

s#

(CNO=‘C3’

(S-C))

Set Difference

Page 20: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-20

Database System-dww

General Form

R S

where R and S are RelationsResult Contains all Tuples that are in R and S.

R S = R – (R – S)Again, there Must be Compatibility

Example “find the students who registered for both C1 and C3”

s#(CNO=‘C1’

(S-C)) s#

(CNO=‘C3’

(S-C))

Set Intersection

Page 21: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-21

Database System-dww

Union, Difference, Intersection Examples

What are these OtherThree Result Tables?

STUDENT - INSTRUCTOR

STUDENT INSTRUCTOR

INSTRUCTOR - STUDENT

Page 22: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-22

Database System-dww

Given RelationsR of Degree k1 and Cardinality card1

S of Degree k2 and Cardinality card2

Cartesian Product R S

is a Relation of Degree (k1+ k2) and Consists of Tuples of Degree (k1+ k2) where each Tuple is a Concatenation of one Tuple of R with one Tuple of S

Cardinality of the Result of the Cartesian Product R S is card1 * card2

What is One Problem with Cartesian Product w.r.t. the Result Set?

Cartesian Product

Page 23: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-23

Database System-dww

Cartesian Product: Example

A B C

a1a2a3

b1b1b4

c3c5c7

FE

f1f5

e1e2

A B C E

a1a1a2a2a3a3

b1b1b1b1b4b4

c3c3c5c5c7c7

e1e2e1e2e1e2

R S

R S F

f1f5f1f5f1f5

Page 24: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-24

Database System-dww

Given R(A1, …,An) and S(B1,…,Bm), the result of a Cartesian product R S is a relation of schema

R’(A1, …, An, B1, …, Bm).Example

“Get a list containing (S#, C#) for all students who live in Storrs but are not registered for the database course”

Cartesian Product

(S#(

city=‘Storrs’(STUDENT))

C#

(CNAME=‘Database’

(COURSE)))– S#, C#

(S-C)

Page 25: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-25

Database System-dww

Cartesian Product Example

ENO ENAME EMP.TITLE SAL.TITLE SAL

E1 J. Doe Elect. Eng.E1 J. Doe Elect. Eng.E1 J. Doe Elect. Eng.E1 J. Doe Elect. Eng.

Elect. Eng. 40000Syst. Anal. 34000Mech. Eng. 27000Programmer 24000

E2 M. Smith Syst. Anal.E2 M. Smith Syst. Anal.E2 M. Smith Syst. Anal.E2 M. Smith Syst. Anal.

Elect. Eng. 40000Syst. Anal. 34000Mech. Eng. 27000Programmer 24000Elect. Eng. 40000Syst. Anal. 34000Mech. Eng. 27000Programmer 24000

Elect. Eng. 40000Syst. Anal. 34000Mech. Eng. 27000Programmer 24000

E3 A. Lee Mech. Eng.E3 A. Lee Mech. Eng.E3 A. Lee Mech. Eng.E3 A. Lee Mech. Eng.

E8 J. Jones Syst. Anal.E8 J. Jones Syst. Anal.E8 J. Jones Syst. Anal.E8 J. Jones Syst. Anal.

EMP SAL

ENO ENAME TITLE

E1 J. Doe Elect. Eng

E2 M. Smith Syst. Anal.

E3 A. Lee Mech. Eng.

E4 J. Miller Programmer

E5 B. Casey Syst. Anal.

E6 L. Chu Elect. Eng.

E7 R. Davis Mech. Eng.

E8 J. Jones Syst. Anal.

EMP

TITLE SAL

SAL

Elect. Eng. 40000

Syst. Anal. 34000

Mech. Eng. 27000

Programmer 24000

Page 26: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-26

Database System-dww

The Join Operation

Used to combine related tuples from two relation into single tuples with some conditionCartesian product all combination of tuples are included in the result, meanwhile in the join operation, only combination of tuples satisfying the join condition appear in the result

Page 27: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-27

Database System-dww

General FormR S

whereR, S are Relations, is a Boolean Expression, called a Join Condition.

A Derivative of Cartesian ProductR S = (R S)

R(A1, A2, ..., Am, B1, B2, ..., Bn) is the Resulting Schema of a -Join over R1 and R2:

R1(A1, A2, ..., Am) R2 (B1, B2, ..., Bn)

Theta Join (-Join)

Page 28: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-28

Database System-dww

A -Join Condition is a Boolean Expression of the form F1 1F2 2n-1Fq (q>=1), where

Fi (i=1,…,q) are Atomic Boolean Expressions of the form

Ai Bj,

Ai, Bj are Attributes of R1 and R2 Respectively

is one of the Algorithmic Comparison Operators =, <>, >, <. >=, <= The Operatori (i=1,…,n-1) is Either a Logical AND operator or a logical OR operator

-Join Condition

Page 29: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-29

Database System-dww

-Join Example

ENO ENAME TITLE

E1 J. Doe Elect. Eng

E2 M. Smith Syst. Anal.

E3 A. Lee Mech. Eng.

E4 J. Miller Programmer

E5 B. Casey Syst. Anal.

E6 L. Chu Elect. Eng.

E7 R. Davis Mech. Eng.

E8 J. Jones Syst. Anal.

EMP

TITLE SAL

SAL

Elect. Eng. 40000

Syst. Anal. 34000

Mech. Eng. 27000

Programmer 24000

ENO ENAME

E1 J. Doe

M. SmithE2

E3 A. Lee

E4 J. Miller

E5 B. Casey

E6 L. Chu

E7 R. DavisE8 J. Jones

TITLE

Elect. Eng.

Analyst

Mech. Eng.

Programmer

Syst. Anal.

Elect. Eng.

Mech. Eng.Syst. Anal.

SAL

40000

34000

27000

24000

34000

40000

2700034000

EMP E.TITLE=SAL.TITLE SAL

SAL.TITLE

Elect. Eng.

Analyst

Mech. Eng.

Programmer

Syst. Anal.

Elect. Eng.

Mech. Eng.Syst. Anal.

Page 30: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-30

Database System-dww

Other Types of Join

Equi-join (EQUIJOIN)The Expression only Contains one or more Equality Comparisons Involving Attributes from R1 and R2

Natural JoinDenoted as R SSpecial Equi-join of Two Relations R and S Over a Set of Attributes Common to both R and SBy Common, it means that each Join Attribute in A has not only Compatible Domains but also the Same Name in both Relations R and S

Page 31: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-31

Database System-dww

Examples

A B C

a1a2a3

b1b1b4

c3c5c7

B E

b1b5

e1e2

R S

R R.B=S.B S

A R.B

a1a2

b1b1

C E

c3c5

e1e1

S.B

b1b1

EQUIJOIN

A R.B C E

a1a2

b1b1

c3c5

e1e1

R SNatural Join

Page 32: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-32

Database System-dww

Natural Join

Natural Join Combines Relations on Attributes with the Same Names

STUDENT(S#, SN, CITY, Email) SC(S#, C#, G)

Example Query 1: “list of students with complete course grade info” STUDENT SC

Page 33: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-33

Database System-dww

Natural Join

All Natural Joins can be Expressed by a Combination of Primitive OperatorsExample Query 2:

“print all students info (courses taken and grades)”

Page 34: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-34

Database System-dww

Natural Join Example

ENO ENAME TITLE

E1 J. Doe Elect. Eng

E2 M. Smith Syst. Anal.

E3 A. Lee Mech. Eng.

E4 J. Miller Programmer

E5 B. Casey Syst. Anal.

E6 L. Chu Elect. Eng.

E7 R. Davis Mech. Eng.

E8 J. Jones Syst. Anal.

EMP

TITLE SAL

SAL

Elect. Eng. 70000

Syst. Anal. 80000

Mech. Eng. 56000

Programmer 60000

ENO ENAME E.TITLE SAL

E1 J. Doe Elect. Eng. 70000

E2 M. Smith Syst. Anal. 80000

56000

80000

E3 A. Lee Mech. Eng.

E8 J. Jones Syst. Anal.

EMP SAL

60000E4 J. Miller Programmer

80000E5 B.Casey Syst.Anal

70000E6 L. Chu Elect.Eng

56000E7 R.Davis Mech.Eng

Page 35: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-35

Database System-dww

Page 36: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-36

Database System-dww

Another Join Example

Page 37: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-37

Database System-dww

Given RelationsR(T,U) of degree rS(U) of degree s

The Division of R by S, R ÷ S

Results is a Relation of Degree (rs) Consists of all (rs)-tuples t such that

for all s-tuples u in S, the tuple tu is in R.

Quotient (Division)

Page 38: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-38

Database System-dww

Division Example

ENO

E3

R ÷ S

ENO PNO PNAME

E1 P1 Instrumentation 150000

BUDGET

E2 P1 Instrumentation 150000E2 P2 Database Develop. 135000E3 P1 InstrumentationE3 P4 MaintenanceE4 P2 InstrumentationE5 P2 InstrumentationE6 P4E7 P3 CAD/CAME8 P3 CAD/CAM

310000150000150000310000250000250000

R

Maintenance

150000

S

PNO PNAME BUDGET

P1 Instrumentation 150000P4 Maintenance 310000

Find the employees who work for both project P1 and project P4?

Page 39: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-39

Database System-dww

Page 40: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-40

Database System-dww

Page 41: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-41

Database System-dww

Division: Another Example

“list the S# of students that have taken all of the courses listed in SC”

S#

(SCC#

(SC))

“list the S# of students who have taken all of the courses taught by instructor Smith”

S#

(SCC#

(Instructor=‘Smith’(SC)))

Page 42: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-42

Database System-dww

Relational Algebra

Fundamental OperatorsDerivable from the fundamental operators

SelectionProjectionUnionDifferenceCartesian Product

Intersection Join, Equi-join, Natural

Join Quotient (Division)

Page 43: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-43

Database System-dww

A Set of Relational Algebra Operations Is Called a Complete Set, If and Only If

Any Relational Algebra Operator in the Set Cannot be Derived in Terms of a Sequence of Others in SetAny Relational Algebra Operator Not in the Set Can Be Derived in Terms of a Sequence of Only the Operators in the Set

All Relational Algebra Operations

Page 44: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-44

Database System-dww

Important Concepts:The Set of Algebra Operations {, , , –,

} is a Complete Set of Relational Algebra Operations

Any Query Language Equivalent to These Five Operations is Called Relationally Complete

All Relational Algebra Operations

Page 45: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-45

Database System-dww

Additional Relational Operations

Aggregate Functions and Grouping

A type of request that cannot be expressed in the basic relational algebra is to specify mathematical aggregate functions on collections of values from the database.

Examples of such functions include retrieving the average or total salary of all employees or the total number of employee tuples. These functions are used in simple statistical queries that summarize information from the database tuples.

Common functions applied to collections of numeric values include SUM, AVERAGE, MAXIMUM, and MINIMUM. The COUNT function is used for counting tuples or values.

Page 46: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-46

Database System-dww

Additional Relational Operations (cont.)

Page 47: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-47

Database System-dww

Use of the Functional operator ℱ

ℱMAX Salary (Employee) retrieves the maximum salary value from the Employee relation

ℱMIN Salary (Employee) retrieves the minimum Salary value from the Employee relation

ℱSUM Salary (Employee) retrieves the sum of the Salary from the Employee relation

DNO ℱCOUNT SSN, AVERAGE Salary (Employee) groups employees by DNO (department number) and computes the count of employees and average salary per department.[ Note: count just counts the number of rows, without removing duplicates]

Additional Relational Operations (cont.)

Page 48: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-48

Database System-dww

Page 49: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-49

Database System-dww

Page 50: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-50

Database System-dww

Additional Relational Operations (cont.)

Recursive Closure Operations

Another type of operation that, in general, cannot be specified in the basic original relational algebra is recursive closure. This operation is applied to a recursive relationship.

Example: retrieve all SUPERVISEES of an EMPLOYEE e at all levels—that is, all EMPLOYEE e’ directly supervised by e; all employees e’’ directly supervised by each employee e’; all employees e’’’ directly supervised by each employee e’’; and so on .

Although it is possible to retrieve employees at each level and then take their union, we cannot, in general, specify a query such as “retrieve the supervisees of ‘James Borg’ at all levels” without utilizing a looping mechanism.

The SQL3 standard includes syntax for recursive closure.

Page 51: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-51

Database System-dww

Additional Relational Operations (cont.)

Page 52: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-52

Database System-dww

Additional Relational Operations (cont.)

The OUTER JOIN Operation

In NATURAL JOIN tuples without a matching (or related) tuple are eliminated from the join result. Tuples with null in the join attributes are also eliminated. This amounts to loss of information.

A set of operations, called outer joins, can be used when we want to keep all the tuples in R, or all those in S, or all those in both relations in the result of the join, regardless of whether or not they have matching tuples in the other relation.

The left outer join operation keeps every tuple in the first or left relation R in R S; if no matching tuple is found in S, then the attributes of S in the join result are filled or “padded” with null values.

A similar operation, right outer join, keeps every tuple in the second or right relation S in the result of R S.

A third operation, full outer join, denoted by keeps all tuples in both the left and the right relations when no matching tuples are found, padding them with null values as needed.

Page 53: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-53

Database System-dww

Additional Relational Operations (cont.)

TEMP (EMPLOYEE SSN=MGRSSN DEPARTMENT

RESULT FNAME, MINIT, LNAME, DNAME (TEMP)

Page 54: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-54

Database System-dww

Additional Relational Operations (cont.)

OUTER UNION Operations

The outer union operation was developed to take the union of tuples from two relations if the relations are not union compatible.

This operation will take the union of tuples in two relations R(X, Y) and S(X, Z) that are partially compatible, meaning that only some of their attributes, say X, are union compatible.

The attributes that are union compatible are represented only once in the result, and those attributes that are not union compatible from either relation are also kept in the result relation T(X, Y, Z).

Page 55: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-55

Database System-dww

Page 56: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-56

Database System-dww

Page 57: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-57

Database System-dww

Additional Relational Operations (cont.)

OUTER UNION Operations

Example: An outer union can be applied to two relations whose schemas are STUDENT(Name, SSN, Department, Advisor) and INSTRUCTOR(Name, SSN, Department, Rank).

Tuples from the two relations are matched based on having the same combination of values of the shared attributes—Name, SSN, Department. If a student is also an instructor, both Advisor and Rank will have a value; otherwise, one of these two attributes will be null.The result relation STUDENT_OR_INSTRUCTOR will have the following attributes:STUDENT_OR_INSTRUCTOR (Name, SSN, Department, Advisor, Rank)

Page 58: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-58

Database System-dww

Page 59: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-59

Database System-dww

Q1: Retrieve the name and address of all employees who work for the ‘Research’ department.

RESEARCH_DEPT DNAME=’Research’ (DEPARTMENT)

RESEARCH_EMPS (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE)

RESULT FNAME, LNAME, ADDRESS (RESEARCH_EMPS)

This query could be specified in other ways; for example, the order of the JOIN and SELECT could be reversed, or the JOIN could be replaced by a NATURAL JOIN after renaming one of the join attributes.

RESULT FNAME, LNAME, ADDRESS ( DNAME=’Research’

(DEPARTMENT) DNUMBER= DNOEMPLOYEE))

Page 60: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-60

Database System-dww

Q2: Retrieve the names of employees who have no dependents.

ALL_EMPS SSN(EMPLOYEE)

EMPS_WITH_DEPS(SSN) ESSN(DEPENDENT)

EMPS_WITHOUT_DEPS (ALL_EMPS - EMPS_WITH_DEPS)

RESULT LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)

RESULT LNAME, FNAME (( SSN(EMPLOYEE) - ESSN(DEPENDENT)) * EMPLOYEE)

Page 61: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-61

Database System-dww

Q3: List the name of all employees with two or more dependents

T1 (SSN, NO_OF_DEPT)

ESSN ℱ COUNT DEPENDENT_NAME (DEPENDENT)

T2 NO_OF_DEPS >= 2 (T1)

RESULT LNAME, FNAME (T2 * EMPLOYEE)

Page 62: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-62

Database System-dww

Exercises

Page 63: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-63

Database System-dww

From the above schema,

(a) Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project.(b) List the names of employees who have a dependent with the same first name as themselves.(c) Find the names of employees that are directly supervised by 'Franklin Wong'. (d) For each project, list the project name and the total hours per week (by all employees) spent on that project.(e) Retrieve the names of employees who work on every project.

Page 64: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-64

Database System-dww

Exercises(f) Retrieve the names of employees who do not work on any project.(g) For each department, retrieve the department name, and the average salary of

employees working in that department.(h) Retrieve the average salary of all female employees.(i) Find the names and addresses of employees who work on at least one project located in Houston but whose department has no location in Houston.List the last names of department managers who have no dependents.

Page 65: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-65

Database System-dww

Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project.

EMPLOYEE_WORKS_ON_ProductX (PNAME=‘ProductX’(PROJECT) PNUMBER=PNO(WORKS_ON))

EMPLOYEE_WORKS_MORE_10 ((EMPLOYEE) SSN=ESSN(HOURS>10(EMPLOYEE_WORKS_ON_ProductX)))

RESULT FNAME,LNAME,ADDRESS( EMPLOYEE_WORKS_MORE_10)

Page 66: The Relational Algebra Database System-dww. Slide 6a-2 Database System-dww Chapter Outline Fundamental Operator Deliverable & Additional Operator.

Slide 6a-66

Database System-dww

EMPLOYEE_FNAME_SAME_DEPENDENT_NAME ((EMPLOYEE) (SSN,FNAME)=(ESSN,DEPENDENT_NAME)(DEPENDENT))

RESULT FNAME,LNAME(EMPLOYEE_FNAME_SAME_DEPENDENT_NAME)

List the names of employees who have a dependent with the same first name as themselves