chapter 6 relational algebra. what is an “algebra” mathematical system consisting of: operands...

53
Chapter 6 Relationa l Algebra

Upload: chastity-morris

Post on 14-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6

Relational Algebra

Page 2: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

What is an “Algebra”

Mathematical system consisting of: Operands --- variables or values from which new

values can be constructed. Operators --- symbols denoting procedures that

construct new values from given values.

Page 3: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

What is Relational Algebra? An algebra whose operands are relations or variables that repre

sent relations. The inputs and outputs of a query are relations.

The basic set of operations for the relational model is known as the relational algebra (RA). These operations enable a user to specify basic retrieval requests.

RA consist of a set of operations that takes one or more relations as input and produce a new relation as their result.

The result of a retrieval is a new relation, which may have been formed from one or more relations. The algebra operations thus produce new relations, which can be further manipulated using operations of the same algebra.

A sequence of RA operations forms a RA expression, whose result will also be a relation that represents the result of a database query (or retrieval request).

Page 4: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

name age sex major

tom 18 m cs

lucy

18 m cs

lily 19 f cs

name sex

tom m

lucy m

RA

Page 5: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Relational Algebra

Union, intersection, and difference. Usual set operations, but require both operands have

the same relation schema. Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations.

Page 6: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Operators

运算符 含义 运算符 含义

集合运算符

并差交

广义笛卡尔积

比较运算符

>

>=

<

<=

=

大于大于等于

小于小于等于

等于不等于

专门的关系

运算符

选择投影连接

逻辑运算符

非与或

Page 7: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Projection L (R)

This operation selects certain columns from the table and discards the other columns. The PROJECT creates a vertical partitioning – one with the needed columns (attributes) containing results of the operation and other containing the discarded Columns.

L is a list of attributes from the schema of R. The result is a new relation that has only some of R’s columns. The project operation removes any duplicate tuples, so the result of

the project operation is a set of tuples and hence a valid relation.

Example

title, year, length (Movie)

SEX(EMPLOYEE) yields only two tuples.

Page 8: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

∏LNAME, FNAME,SALARY(EMPLOYEE)

Page 9: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

The project operation removes any duplicate tuples, so the result of the project operation is a set of tuples and hence a valid relation.

Page 10: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Selection

C (R)SELECT operation is used to select a subset of the tuples from

a relation that satisfy a selection condition. It is a filter that keeps only those tuples that satisfy a condition – those satisfying the condition are selected while others are discarded

In general, the select operation is denoted by <selection condition>(R) where the symbol (sigma) is used to denote the select operator, and the selection condition is a Boolean expression specified on the attributes of relation R.

Example lentgh>=100 AND studioName = ‘Fox’ (Movie)

Page 11: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

s(DNO=4 AND SALARY>25000) OR (DNO=5 AND SLARY>30000)(EMPLOYEE).

Page 12: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-12

The selection condition can have one of the following forms: If U:= C1 S and V:= C2 S simple-selection-condition selection-condition AND selection-condition (i.e.

C1 AND C2 ,which is the same as U V ) selection-condition OR selection-condition (i.e.

C1 OR C2 ,which is the same as U V) NOT selection-condition (i.e. NOT C1 S , which

means as S-U) A simple selection can be any of the following: Attribute oper constant || attribute oper attribute

( where oper can be any comparison operator: =, <>, >, >=, <, and <=)

Page 13: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-13

SELECT Operation Properties The SELECT operation <selection condition>(R) produces a relation

S that has the same schema as R

The SELECT operation is commutative; i.e., <condition1>(< condition2> ( R)) = <condition2> (< condition1> ( R))

A cascaded SELECT operation may be applied in any order; i.e.,

<condition1>(< condition2> (<condition3> ( R)) = <condition2> (< condition3> (< condition1> ( R)))

A cascaded SELECT operation may be replaced by a single selection with a conjunction of all the conditions; i.e.,

<condition1>(< condition2> (<condition3> ( R)) = <condition1> AND < condition2> AND < condition3> ( R)))

Page 14: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Set Operators

R U S: union, the set of elements that are in R or S or both.

R S: intersection, the set of elements that are in both R and S.

R - S: difference, the set of elements that are in R but not in S.

Required R and S must have schema with the same set of

attributes, and Before calculation, the columns of R and S must be

ordered.

Page 15: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

RSA B C

a1 b1 c1

a1 b2 c2

a2 b2 c1

a1 b3 c2

RSA B C

a1 b2 c2a2 b2 c1

R-S

A B C

a1 b1 c1

A   B   C

a1 b1 c1

a1 b2 c2a1 b2 c2

a2 b2 c1

R

A   B  C

a1 b2 c2a1 b2 c2

a1 b3 c2

a2 b2 c1

S

Page 16: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-16

Relational Algebra Operations From Set

Theory UNION Operation

Example: To retrieve the social security numbers of all employees who either work in department 5 or directly supervise an employee who works in department 5, we can use the union operation as follows:

DEP5_EMPS DNO=5 (EMPLOYEE)

RESULT1 SSN(DEP5_EMPS)

RESULT2(SSN) SUPERSSN(DEP5_EMPS)

RESULT RESULT1 RESULT2

The union operation produces the tuples that are in either RESULT1 or RESULT2 or both. The two operands must be “type compatible”.

Page 17: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-17

Relational Algebra Operations FromSet Theory UNION Example

STUDENTINSTRUCTOR

Page 18: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-18

Relational Algebra Operations From Set Theory

Page 19: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

FIGURE≈6.4The set operations UNION, INTERSECTION, and MINUS. (a)

Two union-compatible relations. (b) STUDENT INSTRUCTOR. (c) STUDENT INSTRUCTOR. (d) STUDENT

– INSTRUCTOR. (e) INSTRUCTOR – STUDENT

Page 20: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Cartesian Product or just product R S

This operation is used to combine tuples from two relations in a combinatorial fashion. In general, the result of R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm) is a relation Q with degree n + m attributes Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order. The resulting relation Q has one tuple for each combination of tuples—one from R and one from S.

Hence, if R has nR tuples , and S has nS tuples, then | R x S | will have nR * nS tuples.

The two operands do NOT have to be "type compatible” But beware attribute A of the same name in R and S: use R.A a

nd S.A.

Page 21: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

RS A B C A B C

a1 b1 c1 a1 b2 c2

a1 b1 c1 a1 b3 c2

a1 b1 c1 a2 b2 c1

a1 b2 c2 a1 b2 c2

a1 b2 c2 a1 b3 c2

a1 b2 c2 a2 b2 c1

a2 b2 c1 a1 b2 c2

a2 b2 c1 a1 b3 c2

a2 b2 c1 a2 b2 c1

A   B   C

a1 b1 c1

a1 b2 c2a1 b2 c2

a2 b2 c1

R

A   B  C

a1 b2 c2a1 b2 c2

a1 b3 c2

a2 b2 c1

S

Page 22: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-22

Relational Algebra Operations From Set Theory

Page 23: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Exercises

1 retrieve each department name and the department locations

2 retrieve employee’s name and the project worked on

Page 24: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-24

JOIN Operation The sequence of Cartesian product followed by selections

with or without projections is used quite commonly to identify and select related tuples from two relations. This special operation, called JOIN and is denoted by a

The general form of a join operation on two relations R(A1, A2, . . ., An) and

S(B1, B2, . . ., Bm) is: R <join condition>S where R and S can be any relations .

The join condition is of the form: R.An oper S.Bn Where can be any comparison operator: =, <>, >, >=, <,

and <=).

By definition R <join condition>S is equivalent to <condition1>(R×S)

Page 25: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-25

Example: Suppose that we want to retrieve the name of the manager of each department. To get the manager’s name, we need to combine each DEPARTMENT tuple with the EMPLOYEE tuple whose SSN value matches the MGRSSN value in the department tuple. We do this by using the join operation.

DEPT_MGR DEPARTMENT MGRSSN=SSN EMPLOYEE

Page 26: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-26

Types of Join operation 1. THETA JOIN Operation: any of the comparison operator:

=, <>, >, >=, <, and <=)..

2. EQUIJOIN Operation : The most common use of join involves join conditions with equality comparisons only. Such a join, where the only comparison operator used is =, is called an EQUIJOIN. In the result of an EQUIJOIN we always have one or more pairs of attributes (whose names need not be identical) that have identical values in every tuples. The JOIN seen in the previous example was EQUIJOIN.

3. NATURAL JOIN Operation: Because one of each pair of attributes with identical values is superfluous, a new operation called natural join—denoted by * was created to get rid of the second (superfluous) attribute in an EQUIJOIN condition.

Page 27: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Natural Join

A common type of join connects two relations by: Equating attributes of the same name, and Projecting out one copy of each pair of equated attributes.

Called natural join. Denoted: R1 * R2

Page 28: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Results of two NATURAL JOIN operations. (a) PROJ_DEPT PROJECT * DEPT. (b) DEPT_LOCS DEPARTMENT * DEPT_LOCATIONS.

Page 29: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Natural Join Example Relations r, s:

r s

Page 30: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Theta-Join

R C S Take the product R x S. Then apply C to the result.

C can be any boolean-valued condition.

Page 31: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

A B C

a1 b1 5

a1 b2 6

a2 b3 8

a2 b4 12

R

B E

b1 3

b2 7

b3 10

b3 2

b5 2

S

A R.B C S.B E

a1 b1 5 b2 7

a1 b1 5 b3 10

a1 b2 6 b2 7

a1 b2 6 b3 10

a2 b3 8 b3 10

R C<E S A R.B C S.B E

a1 b1 5 b1 3

a1 b2 6 b2 7

a2 b3 8 b3 10

a2 b3 8 b3 2

Equi join

A  B   C  E

a1 b1 5 3

a1 b2 6 7

a2 b3 8 10

a2 b3 8 2

Natural join

R R.B=S.B S

R S

Page 32: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Division Operation

Notation: Suited to queries that include the phrase “for all”.

Let r and s be relations on schemas R and S respectively where

R = (A1, …, Am , B1, …, Bn )

S = (B1, …, Bn)

The result of r s is a relation on schema

R – S = (A1, …, Am)

r s = { t | t R-S (r) u s ( tu r ) }

Where tu means the concatenation of tuples t and u to produce a si

ngle tuple

r s

Page 33: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Division Operation – Example Relations r, s:

r s: A

B

1

2

A B

12311134612

r

s

Page 34: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Another Division Example

A B

aaaaaaaa

C D

aabababb

E

11113111

Relations r, s:

r s:

D

ab

E

11

A B

aa

C

r

s

Page 35: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

DIVISION Operation The DIVISION operation, denoted by ÷, is useful for a special kind of quer

y that sometimes occurs in database applications. An example is "Retrieve the names of employees who work on all the projects that 'John Smith' works on." To express this query using the DIVISION operation, proceed as follows.

1. Retrieve the list of project numbers that 'John Smith' works on in the intermediate relation SMITH_PNOS:

2. Create a relation that includes a tuple <PNO, ESSN> whenever the employee whose social security number is ESSN works on the project whose number is PNO in the intermediate relation SSN_PNOS:

1. Finally, apply the DIVISION operation to the two relations, which gives the desired employees' social security numbers:

Page 36: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-36

DIVISION Operation (cont.)

(a) Dividing SSN_PNOS by SMITH_PNOS. (b) T R ÷ S.

Page 37: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Aggregate Functions and Operations Aggregation function takes a collection of values and returns a si

ngle value as a result.

avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values

Aggregate operation in relational algebra

E is any relational-algebra expression G1, G2 …, Gn is a list of attributes on which to group (can be em

pty) Each Fi is an aggregate function

Each Ai is an attribute name

)()(,,(),(,,, 221121E

nnn AFAFAFGGG

Page 38: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Additional Relational Operations (cont.)See page 167.

R(Dno, No_of_Emp, AvgSal)(DnoℱCountSSN, AverageSalary(Employee))

Page 39: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-39

Use of the Functional operator ℱFormat: <grouping attributes>ℱ<function list>(R)ℱ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 40: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Outerjoin

Suppose we join R C S. A tuple of R that has no tuple of S with which it joins

is said to be dangling. Similarly for a tuple of S.

Outerjoin preserves dangling tuples by padding them with a special NULL symbol in the result.

Page 41: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-41

Additional Relational Operations (cont.)

Page 42: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Outer Join – Example Relation instructor1

Relation teaches1ID course_id

101011212176766

CS-101FIN-201BIO-101

Comp. Sci.FinanceMusic

ID dept_name101011212115151

name

SrinivasanWuMozart

Page 43: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Left Outer Join

Instructor teaches

Outer Join – Example

Join instructor teaches

ID dept_name1010112121

Comp. Sci.Finance

course_id CS-101 FIN-201

name

SrinivasanWu

ID dept_name101011212115151

Comp. Sci.FinanceMusic

course_id CS-101 FIN-201 null

name

SrinivasanWuMozart

Page 44: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Outer Join – Example

Full Outer Join

instructor teaches

Right Outer Join

instructor teaches

ID dept_name1010112121 null

Comp. Sci.Finance

null

course_id CS-101 FIN-201 BIO-101

name

SrinivasanWunull

ID dept_name101011212115151 null

Comp. Sci.FinanceMusicnull

course_id CS-101 FIN-201 null BIO-101

name

SrinivasanWuMozartnull

Page 45: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-45

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.

An example of a recursive operation is to 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 46: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-46

Additional Relational Operations (cont.)

Page 47: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Combining Operations to Form Query

Algebras allow us to express sequences of operations in a natural way.

Example: in arithmetic --- (x + 4)*(y - 3). Relational algebra allows the same. For example

title, year( lentgh>=100 (Movie) studioName = ‘Fox’ (Movie))

Page 48: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Expressions

Precedence of relational operators:1. Unary operators --- select, project--- have

highest precedence, bind first.2. Then come products and joins.3. Then intersection.4. Finally, union and set difference bind last.

But you can always insert parentheses to force the order you desire.

Page 49: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Expression Trees

Leaves are operands --- either variables standing for relations or particular, constant relations.

Interior nodes are operators, applied to their child or children.

Page 50: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

lentgh >= 100

Movie Movie

studioName = ‘Fox’

title, year

For example:

Page 51: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Chapter 6-51

Examples of Queries in Relational AlgebraQ1: Retrieve the name and address of all employees who

work for the ‘Research’ department. (page 172)

RESEARCH_DEPT DNAME=’Research’ (DEPARTMENT)

RESEARCH_EMPS (RESEARCH_DEPT DNUMBER= DNOEMPLOYEEEMPLOYEE)

RESULT FNAME, LNAME, ADDRESS (RESEARCH_EMPS)

Q6: 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)

Page 52: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

例:学生—课程数据库,包括 Student , Course , SC 三个关系 Sno Sname Ssex Sage Sdept

95001 李勇     男      20      CS    

95002 刘晨     女 19 IS

95003    王敏     女 18 MA

95004   张立     男 19 IS

Student

Cno Cname Cpno Ccredit

1 数据库 5 4

2     数学 2

3   信息系统    1 4

4     操作系统 6 3

5     数据结构 7 4

6     数据处理 2

7      Pascal 语言   6 4

Course

Sno Cno Grade

95001 1 92

95001 2 85

95001 3 88

95002 2 90

95002 3 80

SC

Page 53: Chapter 6 Relational Algebra. What is an “Algebra” Mathematical system consisting of:  Operands --- variables or values from which new values can be

Sname,Sdept(Student)

Sdept = ‘IS’(Student)

Sno( Cno = ‘1’ (SC))

Sno( Cno = ‘1’ or Cno=‘3’ (SC))

Sname ( Cpno = ‘5’ (Course) SC Sno,Sname(Student) )