1 relational algebra operators expression trees. 2 what is an “algebra” umathematical system...

26
1 Relational Algebra Operators Expression Trees

Upload: carlee-telfair

Post on 14-Dec-2015

222 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

1

Relational Algebra

Operators

Expression Trees

Page 2: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

2

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: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

3

What is Relational Algebra?

An algebra whose operands are relations or variables that represent relations.

Operators are designed to do the most common things that we need to do with relations in a database. The result is an algebra that can be used as a

query language for relations.

Page 4: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

4

What we will learn…

Core (or traditionally) relational algebra

Page 5: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

5

Core 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: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

6

Operators

运算符 含义 运算符 含义集合

运算符并差交

广义笛卡尔积

比较运算符

>

>=

<

<=

=

大于大于等于

小于小于等于

等于不等于

专门的关系

运算符

选择投影连接

逻辑运算符

非与或

Page 7: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

7

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 no in S.

Required R and S must have schema with identical set of

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

ordered.

Page 8: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

8

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 9: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

9

Projection

L (R) 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. Eliminate duplicate tuples, if any.

Example

title, year, length (Movie)

Page 10: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

10

Selection

C (R) C is a condition (as in “if” statements) that

refers to attributes of R. The result is a new relation with a subset of

R’s tuples that satisfy C.

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

Page 11: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

11

Cartesian Product or just product R S

Pair each tuple t1 of R with each tuple t2 of S. Result: a new relation with new tuples, each of them

concatenation a pair of t1t2, the attributes of R and S are in ordered.

But beware attribute A of the same name in R and S: use R.A and S.A.

Page 12: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

12

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 13: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

13

Natural Join

A frequent 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 14: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

14

Theta-Join

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

C can be any boolean-valued condition. Historic versions of this operator allowed only A

theta B, where theta was =, <, etc.

Page 15: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

15

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

等值连接

A  B   C  E

a1 b1 5 3

a1 b2 6 7

a2 b3 8 10

a2 b3 8 2

自然连接

R R.B=S.B S

R S

Page 16: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

16

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 17: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

17

Example: Outerjoin

R = A B S = B C1 2 2 34 5 6 7

(1,2) joins with (2,3), but the other two tuplesare dangling.

R OUTERJOIN S = A B C1 2 34 5 NULLNULL 6 7

Page 18: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

18

Dependent and Independent Operations

R C S = C (R x S)

R S = L ( C (R x S))

R S = R – (R – S)

Page 19: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

19

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 20: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

20

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 21: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

21

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 22: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

22

lentgh >= 100

Movie Movie

studioName = ‘Fox’

title, year

For example:

Page 23: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

23

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

95001 李勇     男      20      CS    

95002 刘晨     女 19 IS

95003    王敏     女 18 MA

95004   张立     男 19 IS

Student

Cno Cname Cpqo 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 24: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

24

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

Page 25: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

25

Reading Guide

Required: 4.1 Recommended: 《数据库系统概论》第二章中的关系

代数

Page 26: 1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which

26

练习 图书馆管理数据库

读者 ( 读者编号 , 姓名 , 单位 ) 图书 ( 书号 , 书名 , 作者 , 出版社 , 单价 , 类型 ) 借阅记录 ( 读者编号 , 书号 , 借阅日期 , 应还日期 ) 还书记录 ( 读者编号 , 书号 , 归还日期 )

用关系代数描述以下查询要求 查询“人民邮电出版社”出版的所有图书的相关信息 查询单价在 15 元以上的书名和作者 查询 8 号读者 2003 年 3 月 10 日所借图书的相关信

息 查询超期归还图书的读者姓名和单位 查询借阅过《天龙八部》的读者的信息 查询借阅过“金庸”所有著作的读者的姓名 查询没有借阅过任何图书的读者的姓名