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

Presentation on theme: "1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which."— Presentation transcript:

1 Relational Algebra Operators Expression Trees

2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which new values can be constructed. wOperators --- symbols denoting procedures that construct new values from given values.

3 What is Relational Algebra? uAn algebra whose operands are relations or variables that represent relations. uOperators are designed to do the most common things that we need to do with relations in a database. wThe result is an algebra that can be used as a query language for relations.

4 What we will learn… uCore (or traditionally) relational algebra

5 Core Relational Algebra uUnion, intersection, and difference. wUsual set operations, but require both operands have the same relation schema. uSelection: picking certain rows. uProjection: picking certain columns. uProducts and joins: compositions of relations.

6 Operators 运算符含义运算符含义 集合 运算符 并 差 交 广义笛卡尔积 比较 运算符 > >= < <= = 大于 大于等于 小于 小于等于 等于 不等于 专门的 关系 运算符 选择 投影 连接 除 逻辑 运算符 非与或非与或

7 Set Operators uR U S: union, the set of elements that are in R or S or both. uR S: intersection, the set of elements that are in both R and S. uR - S: difference, the set of elements that are in R but no in S. uRequired wR and S must have schema with identical set of attributes, and wBefore calculation, the columns of R and S must be ordered.

8 RSRS A B C a1 b1 c1 a1 b2 c2 a2 b2 c1 a1 b3 c2 RSRS A B C a1 b2 c2 a2 b2 c1 R-S A B C a1 b1 c1 Ａ Ｂ Ｃ a1 b1 c1 a1 b2 c2 a2 b2 c1 R Ａ Ｂ Ｃ a1 b2 c2 a1 b3 c2 a2 b2 c1 S

9 Projection u  L (R) wL is a list of attributes from the schema of R. wThe result is a new relation that has only some of R’s columns. wEliminate duplicate tuples, if any. uExample  title, year, length (Movie)

10 Selection u C (R) wC is a condition (as in “if” statements) that refers to attributes of R. wThe result is a new relation with a subset of R’s tuples that satisfy C. uExample lentgh>=100 AND studioName = ‘Fox’ (Movie)

11 Cartesian Product uor just product uR  S wPair each tuple t1 of R with each tuple t2 of S. wResult: a new relation with new tuples, each of them concatenation a pair of t1t2, the attributes of R and S are in ordered. wBut beware attribute A of the same name in R and S: use R.A and S.A.

12 RSRS 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 Ａ Ｂ Ｃ a1 b1 c1 a1 b2 c2 a2 b2 c1 R Ａ Ｂ Ｃ a1 b2 c2 a1 b3 c2 a2 b2 c1 S

13 Natural Join uA frequent type of join connects two relations by: wEquating attributes of the same name, and wProjecting out one copy of each pair of equated attributes. uCalled natural join. uDenoted: R1 R2

14 Theta-Join uR C S wTake the product R x S. wThen apply C to the result. uC can be any boolean-valued condition. wHistoric versions of this operator allowed only A theta B, where theta was =, <, etc.

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3396893/slides/slide_15.jpg", "name": "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

16 Outerjoin uSuppose we join R C S. uA tuple of R that has no tuple of S with which it joins is said to be dangling. wSimilarly for a tuple of S. uOuterjoin preserves dangling tuples by padding them with a special NULL symbol in the result.

17 Example: Outerjoin R = ABS =BC 1223 4567 (1,2) joins with (2,3), but the other two tuples are dangling. R OUTERJOIN S =ABC 123 45NULL NULL67

18 Dependent and Independent Operations R C S = C (R x S) R S =  L ( C (R x S)) R S = R – (R – S)

19 Combining Operations to Form Query uAlgebras allow us to express sequences of operations in a natural way. wExample: in arithmetic --- (x + 4)*(y - 3). uRelational algebra allows the same. For example  title, year ( lentgh>=100 (Movie) studioName = ‘Fox’ (Movie))

20 Expressions uPrecedence 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. wBut you can always insert parentheses to force the order you desire.

21 Expression Trees uLeaves are operands --- either variables standing for relations or particular, constant relations. uInterior nodes are operators, applied to their child or children.

22 lentgh >= 100 Movie studioName = ‘Fox’  title, year For example:

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

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

25 Reading Guide uRequired: 4.1 uRecommended: 《数据库系统概论》第二章中的关系 代数

26 练习 u 图书馆管理数据库 w 读者 ( 读者编号, 姓名, 单位 ) w 图书 ( 书号, 书名, 作者, 出版社, 单价, 类型 ) w 借阅记录 ( 读者编号, 书号, 借阅日期, 应还日期 ) w 还书记录 ( 读者编号, 书号, 归还日期 ) u 用关系代数描述以下查询要求 w 查询 “ 人民邮电出版社 ” 出版的所有图书的相关信息 w 查询单价在 15 元以上的书名和作者 w 查询 8 号读者 2003 年 3 月 10 日所借图书的相关信息 w 查询超期归还图书的读者姓名和单位 w 查询借阅过《天龙八部》的读者的信息 w 查询借阅过 “ 金庸 ” 所有著作的读者的姓名 w 查询没有借阅过任何图书的读者的姓名

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

Similar presentations