Download presentation
Presentation is loading. Please wait.
Published byChastity Morris Modified over 9 years ago
1
Chapter 6 Relational Algebra
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.
3
What is Relational Algebra? An algebra whose operands are relations or variables that represent 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).
4
name agesex major tom18mcs luc y 18mcs lily19fcs name sex tomm lucym RA
5
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.
6
Operators 运算符含义运算符含义 集合 运算符 并 差 交 广义笛卡尔积 比较 运算符 > >= < <= = 大于 大于等于 小于 小于等于 等于 不等于 专门的 关系 运算符 选择 投影 连接 除 逻辑 运算符 非与或非与或
7
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.
8
∏ LNAME, FNAME,SALARY(EMPLOYEE)
9
The project operation removes any duplicate tuples, so the result of the project operation is a set of tuples and hence a valid relation.
10
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 (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)
11
s (DNO=4 AND SALARY>25000) OR (DNO=5 AND SLARY>30000) (EMPLOYEE).
12
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 <=)
13
Chapter 6-13 SELECT Operation Properties The SELECT operation (R) produces a relation S that has the same schema as R The SELECT operation is commutative; i.e., ( ( R)) = ( ( R)) A cascaded SELECT operation may be applied in any order; i.e., ( ( ( R)) = ( ( ( R))) A cascaded SELECT operation may be replaced by a single selection with a conjunction of all the conditions; i.e., ( ( ( R)) = AND AND ( R)))
14
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.
15
RSRS A B C a1 b1 c1 a1 b2 c2 a2 b2 c1 a1 b3 c2 RSRS A B C a1 b2 c2 a2 b2 c1 R-S A B C a1 b1 c1 A B C a1 b1 c1 a1 b2 c2 a2 b2 c1 R A B C a1 b2 c2 a1 b3 c2 a2 b2 c1 S
16
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”.
17
Chapter 6-17 Relational Algebra Operations From Set Theory UNION Example STUDENT INSTRUCTOR
18
Chapter 6-18 Relational Algebra Operations From Set Theory
19
FIGURE≈6.4 The set operations UNION, INTERSECTION, and MINUS. (a) Two union- compatible relations. (b) STUDENT INSTRUCTOR. (c) STUDENT INSTRUCTOR. (d) STUDENT – INSTRUCTOR. (e) INSTRUCTOR – STUDENT
20
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(A 1, A 2,..., A n ) x S(B 1, B 2,..., B m ) is a relation Q with degree n + m attributes Q(A 1, A 2,..., A n, B 1, B 2,..., B m ), 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 n R tuples, and S has n S tuples, then | R x S | will have n R * n S 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 and S.A.
21
RSRS 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 c2 a2 b2 c1 R A B C a1 b2 c2 a1 b3 c2 a2 b2 c1 S
22
Chapter 6-22 Relational Algebra Operations From Set Theory
23
Exercises 1 retrieve each department name and the department locations 2 retrieve employee’s name and the project worked on
24
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(A 1, A 2,..., A n ) and S(B 1, B 2,..., B m ) is: R S where R and S can be any relations. The join condition is of the form: R.A n oper S.B n Where can be any comparison operator: =, <>, >, >=, <, and <=). By definition R S is equivalent to (R×S)
25
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
26
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.
27
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
28
Results of two NATURAL JOIN operations. (a) PROJ_DEPT PROJECT * DEPT. (b) DEPT_LOCS DEPARTMENT * DEPT_LOCATIONS.
29
Natural Join Example Relations r, s: r s
30
Theta-Join R C S Take the product R x S. Then apply C to the result. C can be any boolean-valued condition.
31
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
32
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 = (A 1, …, A m, B 1, …, B n ) S = (B 1, …, B n ) The result of r s is a relation on schema R – S = (A 1, …, A m ) r s = { t | t R-S (r) u s ( tu r ) } Where tu means the concatenation of tuples t and u to produce a single tuple r s
33
Division Operation – Example Relations r, s: r s: A B 1212 AB 1231113461212311134612 r s
34
Another Division Example AB aaaaaaaaaaaaaaaa CD aabababbaabababb E 1111311111113111 nRelations r, s: nr s:nr s: D abab E 1111 AB aaaa C r s
35
DIVISION Operation The DIVISION operation, denoted by ÷, is useful for a special kind of query 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 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:
36
Chapter 6-36 DIVISION Operation (cont.) (a) Dividing SSN_PNOS by SMITH_PNOS. (b) T R ÷ S.
37
Aggregate Functions and Operations Aggregation function takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Aggregate operation in relational algebra E is any relational-algebra expression G 1, G 2 …, G n is a list of attributes on which to group (can be empty) Each F i is an aggregate function Each A i is an attribute name
38
Additional Relational Operations (cont.) See page 167. R (Dno, No_of_Emp, AvgSal) ( Dno ℱ Count SSN, Average Salary (Employee))
39
Chapter 6-39 Use of the Functional operator ℱ Format: ℱ (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.)
40
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.
41
Chapter 6-41 Additional Relational Operations (cont.)
42
Outer Join – Example Relation instructor1 n Relation teaches1 IDcourse_id 10101 12121 76766 CS-101 FIN-201 BIO-101 Comp. Sci. Finance Music IDdept_name 10101 12121 15151 name Srinivasan Wu Mozart
43
Left Outer Join Instructor teaches Outer Join – Example Join instructor teaches ID dept_name 10101 12121 Comp. Sci. Finance course_id CS-101 FIN-201 name Srinivasan Wu ID dept_name 10101 12121 15151 Comp. Sci. Finance Music course_id CS-101 FIN-201 null name Srinivasan Wu Mozart
44
Outer Join – Example Full Outer Join instructor teaches Right Outer Join instructor teaches ID dept_name 10101 12121 null Comp. Sci. Finance null course_id CS-101 FIN-201 BIO-101 name Srinivasan Wu null ID dept_name 10101 12121 15151 null Comp. Sci. Finance Music null course_id CS-101 FIN-201 null BIO-101 name Srinivasan Wu Mozart null
45
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.
46
Chapter 6-46 Additional Relational Operations (cont.)
47
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))
48
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. w But you can always insert parentheses to force the order you desire.
49
Expression Trees Leaves are operands --- either variables standing for relations or particular, constant relations. Interior nodes are operators, applied to their child or children.
50
lentgh >= 100 Movie studioName = ‘Fox’ title, year For example:
51
Chapter 6-51 Examples of Queries in Relational Algebra Q1: 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= DNOEMPLOYEE EMPLOYEE) 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)
52
例:学生 — 课程数据库,包括 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
53
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) )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.