Database Systems Chapter 6 ITM 354. 2 Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.

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)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
Chapter 6 Additional Relational Operations Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
The Relational Algebra
Copyright © 2004 Pearson Education, Inc.. Chapter 6 The Relational Algebra and Relational Calculus.
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.
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.
Lecture 15: Relational Algebra
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.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 6 The Relational Algebra University Of Palestine.
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 Part a The Relational Algebra and Relational Calculus Hours 1,2.
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.
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational 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.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Lecture 8 Relational Algebra April 26, 2015.
Database Systems Chapter 6
The Relational Algebra and Relational Calculus
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
Chapter 6: Relational Algebra Fall 2015 Dr. Abdullah Almutairi
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Chapter 4 The Relational Algebra and Calculus
The Relational Algebra and Calculus
The Relational Algebra and Relational Calculus
The Relational Algebra
The Relational Algebra and The Relational Calculus
Presentation transcript:

Database Systems Chapter 6 ITM 354

2 Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification of basic retrievals The result of a retrieval is a new relation, which may have been formed from one or more relations. – algebra operations thus produce new relations, which can be further manipulated the same algebra. A sequence of relational algebra operations forms a relational algebra expression, –the result will also be a relation that represents the result of a database query (or retrieval request).

3 What is an Algebra? A language based on operators and a domain of values Operators map values taken from the domain into other domain values Hence, an expression involving operators and arguments produces a value in the domain relational algebraWhen the domain is a set of all relations we get the relational algebra

4 Relational Algebra Definitions Domain: set of relations selectprojectunionset differenceCartesianproductBasic operators: select, project, union, set difference, Cartesian (cross) product set intersectiondivision joinDerived operators: set intersection, division, join Procedural: Relational expression specifies query by describing an algorithm (the sequence in which operators are applied) for determining the result of an expression

5 Unary Relational Operations SELECT Operation: 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 qualifying condition. Examples:  DNO = 4 (EMPLOYEE)  SALARY > 30,000 (EMPLOYEE) –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

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

7 Selection Condition Operators:, =,  Simple selection condition: – operator – AND – OR – NOT

8 Select Examples Person 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps Id Name Address Hobby Person  Id>3000 OR Hobby=‘hiking’ (Person) Person  Id>3000 AND Id <3999 (Person) Person  NOT (Hobby=‘hiking’) (Person) Person  Hobby  ‘hiking’ (Person)

9 Unary Relational Operations (cont.) PROJECT Operation: selects certain columns from the table and discards the others. Example:   LNAME, FNAME,SALARY (EMPLOYEE) The general form of the project operation is:  (R) where  is the symbol used to represent the project operation and is the desired list of attributes. PROJECT removes duplicate tuples, so the result is a set of tuples and hence a valid relation.

10 The number of tuples in the result of   R  is always less or equal to the number of tuples in R. If attribute list includes a key of R, then the number of tuples is equal to the number of tuples in R.     R  )    R  as long as   contains the  attributes in   PROJECT Operation Properties

SELECT and PROJECT Operations (a)  (DNO=4 AND SALARY>25000) OR (DNO=5 AND SALARY>30000) (EMPLOYEE) (b)  LNAME, FNAME, SALARY (EMPLOYEE) (c)  SEX, SALARY (EMPLOYEE)

Relational Algebra Operations from Set Theory The UNION, INTERSECTION, and MINUS Operations The CARTESIAN PRODUCT (or CROSS PRODUCT) Operation

13 Set Operators A relation is a set of tuples, so set operations apply: , ,  (set difference) Result of combining two relations with a set operator is a relation => all elements are tuples with the same structure

14 UNION Operation Denoted by R  S Result is a relation that includes all tuples that are either in R or in S or in both. Duplicate tuples are eliminated. Example: Retrieve the SSNs of all employees who either work in department 5 or directly supervise an employee who works in department 5: 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”.

15 Type (Union) Compatibility The operand relations R 1 (A 1, A 2,..., A n ) and R 2 (B 1, B 2,..., B n ) must have the same number of attributes, and the domains of corresponding attributes must be compatible, i.e. –dom(A i ) = dom(B i ) for i=1, 2,..., n. UNION Operation

16 Example Tables: Person Person (SSN, Name, Address, Hobby) Professor Professor (Id, Name, Office, Phone) are not union compatible. But PersonProfessor  Name (Person) and  Name (Professor) are union compatible so PersonProfessor  Name (Person) -  Name (Professor) makes sense.

17 STUDENT  INSTRUCTOR: UNION Example What would STUDENT  INSTRUCTOR be?

18 Set Difference (or MINUS) Operation The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S. The two operands must be "type compatible”. Set Difference Operation

19 Set Difference Example SIDSNameAge 202Rusty21 403Marcia20 914Hal24 192Jose22 881Stimpy19 SIDSNameAge 473Popeye22 192Jose22 715Alicia28 914Hal24 S1S2

20 Relational Algebra Operations From Set Theory (cont.) Union and intersection are commutative operations: R  S = S  R, and R  S = S  R Both union and intersection can be treated as n-ary operations applicable to any number of relations as both are associative operations; that is R  (S  T) = (R  S)  T, and (R  S)  T = R  (S  T) The minus operation is not commutative; that is, in general R - S ≠ S – R

21 Cartesian (Cross) Product RSRS R SIf R and S are two relations, R  S is the set of all concatenated tuples, where x is a tuple in R and y is a tuple in S –RS –R and S need not be union compatible RSR  S is expensive to compute: –Factor of two in the size of each row; Quadratic in the number of rows A B C D A B C D x1 x2 y1 y2 x1 x2 y1 y2 x3 x4 y3 y4 x1 x2 y3 y4 x3 x4 y1 y2 RS R S x3 x4 y3 y4 RS R  S

Cartesian Product Example We want a list of COMPANY’s female employees dependents.

Binary Relational Operations: JOIN and DIVISION The JOIN Operation The EQUIJOIN and NATURAL JOIN variations of JOIN The DIVISION Operation

25 JOIN Operation Cartesian product followed by select is commonly used to identify and select related tuples from two relations => called JOIN. It is denoted by a –This operation is important for any relational database with more than a single relation, because it allows us to process relationships among relations. –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 that result from general relational algebra expressions.

DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE The Binary Join Operation

27 EQUIJOIN & NATURAL JOIN EQUIJOIN –most common join: join conditions with equality comparisons only. –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 tuple. –The JOIN in the previous example was EQUIJOIN. NATURAL JOIN –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. –The standard definition of natural join requires that each pair of corresponding join attributes, have the same name in both relations. If this is not the case, a renaming operation is applied.

(a) PROJ_DEPT  PROJECT * DEPT (b) DEPT_LOCS  DEPARTMENT * DEPT_LOCATIONS Natural Join Operations

The DIVISION Operation (a) Dividing SSN_PNOS by SMITH_PNOS. (b) T  R ÷ S.

Additional Relational Operations Aggregate Functions and Grouping Recursive Closure Operations The OUTER JOIN Operation

Aggregate Functions E.g. SUM, AVERAGE, MAX, MIN, COUNT

Recursive Closure Example

33 OUTER JOINs In NATURAL JOIN tuples without a matching (or related) tuple are eliminated from the join result. Tuples with null in the join attributes are also eliminated. This loses information. Outer joins, can be used when we want to keep all the tuples in R, all those in S, or all those in both relations –regardless of whether they have matching tuples in the other relation. The left outer join operation keeps every tuple in the first or left relation R in R S; if no matching tuple is found in S, then the attributes of S in the join result are “padded” with null values. A similar operation, right outer join, keeps every tuple in the second or right relation S in the result of R S. A third operation, full outer join, denoted by keeps all tuples in both the left and the right relations when no matching tuples are found, padding them with null values as needed.

Left Outer Join E.g. List all employees and the department they manage, if they manage a department.

Examples of Queries in Relational Algebra Work through Query 1 (p. 171) and Query 2 (p. 172) of E&N Show all intermediate relations as well as the final result