Chapter 6 The Relational Algebra and Relational Calculus Fundamentals of Database Systems, 6/e The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 1 Fundamentals of Databases Topics so far Recap of Previous Content of This Summary & Checklist Recap of Previous DB and DBMS Example of Database : Student and Course information Schema Diagram Network Model for Schema Diagram Example of Database : COMPANY Relational Schema vs. ER Model Relational Schema vs. ER Model vs. UML Class Diagram Relational Model Concepts Relational Schema of COMPANY ER Schema of COMPANY ER Diagrams, Naming Conventions, and Design Issues UML Schema of COMPANY The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 2 Web Systems, Dr. Salha Alzahrani 1
Fundamentals of Databases Objectives Recap of Previous Content of This Summary & Checklist Objectives of By the end of this lecture, you will be able to: Differentiate between Unary and Binary relational operations Use SELECT and PROJECT Use set theory operations Use JOIN and DIVISION Apply and solve examples of relational algebra operations. The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 3 Fundamentals of Databases Content of Lect. Recap of Previous Content of This Summary & Checklist Content of 1. Unary Relational Operations: SELECT and PROJECT 2. Relational Algebra Operations from Set Theory 3. Binary Relational Operations: JOIN and DIVISION 4. Variations of JOIN: The EQUIJOIN, NATURAL JOIN, Inner Join 5. More Examples of Queries in Relational Algebra 6. Summary of Operations of Relational Algebra The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 4 Web Systems, Dr. Salha Alzahrani 2
Chapter 6 Introduction Relational algebra Basic set of operations for the relational model Relational algebra expression Sequence of relational algebra operations Relational calculus Higher-leveldeclarative language for specifying relational queries The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 5 1. Unary Relational Operations SELECT PROJECT Unary = Applied to a single relation The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 6 Web Systems, Dr. Salha Alzahrani 3
Chapter 6 1. Unary Relational Operations: SELECT and PROJECT The SELECT Operation Subset of the tuples from a relation that satisfies a selection condition: Boolean expression contains clauses of the form <attribute name> <comparison op> <constant value> or <attribute name> <comparison op> <attribute name> The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 7 SELECT Example Find the list of employees from department 4? The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 8 Web Systems, Dr. Salha Alzahrani 4
SELECT Example Find the list of employees from department 4 whose salaries is above 25,000? The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 9 SELECT Example Find the list of employees from department 4 whose salaries is above 25,000 or from department 5 whose salaries are above 30,000? The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 10 Web Systems, Dr. Salha Alzahrani 5
Chapter 6 1. Unary Relational Operations: SELECT and PROJECT What s going on? <selection condition> applied independently to each individual tuple tin R If condition evaluates to TRUE, tuple selected Boolean conditions AND, OR, and NOT Selectivity Fraction of tuples selected by a selection condition SELECT operation is commutative CascadeSELECT operations into a single operation with AND condition The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 11 Chapter 6 1. Unary Relational Operations: SELECT and PROJECT The PROJECT Operation Selects columns from table and discards the other columns: Degree Number of attributes in <attribute list> Duplicate elimination Result of PROJECT operation is a set of distinct tuples The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 12 Web Systems, Dr. Salha Alzahrani 6
PROJECT Example Find/Retrieve the list of first name, last name and salary of all employees? The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 13 PROJECT Example Find the list of first name, last name and salary of all employees who work in department 5? Sequence of operations In-line expression The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 14 Web Systems, Dr. Salha Alzahrani 7
Chapter 6 2. Relational Algebra Operations from Set Theory UNION, INTERSECTION, MINUS Merge the elements of two sets in various ways Unary? Binary? Relations must have the same type of tuples The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 15 Chapter 6 2. Relational Algebra Operations from Set Theory UNION R S Includes all tuples that are either in Ror in Sor in both R and S Duplicate tuples eliminated INTERSECTION R S Includes all tuples that are in both R and S SET DIFFERENCE (or MINUS) R S Includes all tuples that are in Rbut not in S The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 16 Web Systems, Dr. Salha Alzahrani 8
Chapter 6 2. Relational Algebra Operations from Set Theory CARTESIAN PRODUCT CROSS PRODUCT orcross JOIN Denoted by Binary set operation Relations do not have to be union compatible Useful when followed by a selection that matches values of attributes The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 17 Chapter 6 3. Binary Relational Operations: JOIN and DIVISION The JOIN Operation Denoted by Combine related tuples from two relations into single longer tuples General join condition of the form <condition> AND <condition> AND...AND <condition> The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 18 Web Systems, Dr. Salha Alzahrani 9
Chapter 6 3. Binary Relational Operations: JOIN and DIVISION THETA JOIN Each <condition> of the form A i θ B j A i is an attribute of R B j is an attribute of S A i and B j have the same domain θ (theta) is one of the comparison operators: {=, <,, >,, } The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 19 JOIN Example List the first name, last name and salary of department managers? The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 20 Web Systems, Dr. Salha Alzahrani 10
Chapter 6 3. Binary Relational Operations: JOIN and DIVISION The DIVISION Operation Denoted by Example: retrieve the names of employees who work on all the projects that John Smith works on Apply to relations R(Z) S(X) Attributes of Rare a subset of the attributes of S The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 21 Chapter 6 4. Variations of JOIN: The EQUIJOIN, NATURAL JOIN, Inner Join EQUIJOIN Only = comparison operator used Always have one or more pairs of attributes that have identical values in every tuple NATURAL JOIN Denoted by * Removes second (superfluous) attribute in an EQUIJOIN condition Inner joins Type of match and combine operation Defined formally as a combination of CARTESIAN PRODUCT and SELECTION The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 22 Web Systems, Dr. Salha Alzahrani 11
Chapter 6 5. More Examples of Queries in Relational Algebra The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 23 Chapter 6 5. More Examples of Queries in Relational Algebra The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 24 Web Systems, Dr. Salha Alzahrani 12
Operations of Relational Algebra The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 25 Operations of Relational Algebra The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 26 Web Systems, Dr. Salha Alzahrani 13
Fundamentals of Databases End of Recap of Previous Content of This Summary & Checklist Summary & Checklist Unary Relational Operations: SELECT and PROJECT Relational Algebra Operations from Set Theory Binary Relational Operations: JOIN and DIVISION Variations of JOIN: The EQUIJOIN, NATURAL JOIN, Inner Join More Examples of Queries in Relational Algebra Summary of Operations of Relational Algebra The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 27 More in-class exercises on Relational Algebra s Queries Additional Relational Operations Tuble Relational Calculus Be ready & Read more: https://en.wikipedia.org/wiki/relational_algebra https://en.wikipedia.org/wiki/tuple_relational_calculus http://www.inf.unideb.hu/valseg/dolgozok/ispany/datamin e/alkalmazasok/fundamentals_of_database_systems,_6th_ Edition.pdf Prepare your glossary and academic vocabulary list. Download & print the lecture notes before your class. The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 28 Web Systems, Dr. Salha Alzahrani 14