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

Slides:



Advertisements
Similar presentations
Chapter 6 The Relational Algebra
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
The Relational Algebra
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 4 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
CS 380 Introduction to Database Systems Chapter 7: The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
The Relational Algebra and Calculus
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
October 9, Announcements Exam Tuesday in class –Closed book Program 2 due Friday –Turn in using D2L dropbox –Only turn in the 4 files specified.
Relational Algebra. 2 Outline  Relational Algebra Unary Relational Operations Relational Algebra Operations from Set Theory Binary Relational Operations.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Slide 6- 1 CARTESIAN (or cross) Product Operation Defines a relation Q that is the concatenation of every tuple of relation R with every tuple of relation.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
1 CSE 480: Database Systems Lecture 16: Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض.
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Lecture 8 Relational Algebra April 26, 2015.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Database Systems Chapter 6
The Relational Algebra
The Relational Algebra and Relational Calculus
Chapter (6) The Relational Algebra and Relational Calculus Objectives
The Relational Algebra and Calculus
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
The Relational Algebra and Relational Calculus
Chapter 6: Relational Algebra Fall 2015 Dr. Abdullah Almutairi
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Chapter 4 The Relational Algebra and Calculus
CS4222 Principles of Database System
The Relational Algebra and Calculus
The Relational Algebra and Relational Calculus
The Relational Algebra
The Relational Algebra and The Relational Calculus
Presentation transcript:

Chapter 6 Relational Algebra

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.

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

name agesex major tom18mcs luc y 18mcs lily19fcs name sex tomm lucym RA

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.

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

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.

∏  LNAME, FNAME,SALARY(EMPLOYEE)

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

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)

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

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

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

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.

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

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”.

Chapter 6-17 Relational Algebra Operations From Set Theory UNION Example STUDENT  INSTRUCTOR

Chapter 6-18 Relational Algebra Operations From Set Theory

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

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.

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

Chapter 6-22 Relational Algebra Operations From Set Theory

Exercises 1 retrieve each department name and the department locations 2 retrieve employee’s name and the project worked on

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)

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

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.

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

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

Natural Join Example Relations r, s: r s

Theta-Join R C S  Take the product R x S.  Then apply C to the result. C can be any boolean-valued condition.

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 b a2 b3 8 2 Natural join R R.B=S.B S R S

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

Division Operation – Example Relations r, s: r  s: A B  1212 AB  r s

Another Division Example AB  aaaaaaaaaaaaaaaa CD  aabababbaabababb E nRelations r, s: nr  s:nr  s: D abab E 1111 AB  aaaa C  r s

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: 

Chapter 6-36 DIVISION Operation (cont.) (a) Dividing SSN_PNOS by SMITH_PNOS. (b) T  R ÷ S.

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

Additional Relational Operations (cont.) See page 167.  R (Dno, No_of_Emp, AvgSal) ( Dno ℱ Count SSN, Average Salary (Employee))

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

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.

Chapter 6-41 Additional Relational Operations (cont.)

Outer Join – Example Relation instructor1 n Relation teaches1 IDcourse_id CS-101 FIN-201 BIO-101 Comp. Sci. Finance Music IDdept_name name Srinivasan Wu Mozart

Left Outer Join Instructor teaches Outer Join – Example Join instructor teaches ID dept_name Comp. Sci. Finance course_id CS-101 FIN-201 name Srinivasan Wu ID dept_name Comp. Sci. Finance Music course_id CS-101 FIN-201 null name Srinivasan Wu Mozart

Outer Join – Example Full Outer Join instructor teaches Right Outer Join instructor teaches ID dept_name null Comp. Sci. Finance null course_id CS-101 FIN-201 BIO-101 name Srinivasan Wu null ID dept_name null Comp. Sci. Finance Music null course_id CS-101 FIN-201 null BIO-101 name Srinivasan Wu Mozart null

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.

Chapter 6-46 Additional Relational Operations (cont.)

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

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.

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

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

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)

例:学生 — 课程数据库,包括 Student , Course , SC 三个关系 Sno Sname Ssex Sage Sdept 李勇 男 20 CS 刘晨 女 19 IS 王敏 女 18 MA 张立 男 19 IS Student Cno Cname Cpno Ccredit 1 数据库 数学 2 3 信息系统 操作系统 数据结构 数据处理 2 7 Pascal 语言 6 4 Course Sno Cno Grade SC

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