Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Relational Algebra and Relational Calculus

Similar presentations


Presentation on theme: "The Relational Algebra and Relational Calculus"— Presentation transcript:

1 The Relational Algebra and Relational Calculus
Chapter 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة من الادوات في ذلك. حيث المرجع الاساسي للمادة هي الكتاب المعتمد في وصف المقرر

2 Chapter Outline Relational Algebra Unary Relational Operations
Relational Algebra Operations From Set Theory Binary Relational Operations Additional Relational Operations Examples of Queries in Relational Algebra

3 Binary Relational Operations: EQUIJOIN
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 tuple. The JOIN seen in the previous example was an EQUIJOIN.

4 Binary Relational Operations: NATURAL JOIN Operation
Another variation of JOIN called NATURAL JOIN — denoted by * was created to get rid of the second (superfluous) attribute in an EQUIJOIN condition. because one of each pair of attributes with identical values is superfluous The standard definition of natural join requires that the two join attributes, or each pair of corresponding join attributes, have the same name in both relations

5 Binary Relational Operations NATURAL JOIN (contd.)
Example: To apply a natural join on the DNUMBER attributes of DEPARTMENT and DEPT_LOCATIONS, it is sufficient to write: DEPT_LOCS  DEPARTMENT * DEPT_LOCATIONS Only attribute with the same name is DNUMBER An implicit join condition is created based on this attribute: DEPARTMENT.DNUMBER=DEPT_LOCATIONS.DNUMBER Another example: Q  R(A,B,C,D) * S(C,D,E) The implicit join condition includes each pair of attributes with the same name, “AND”ed together: R.C=S.C AND R.D.S.D Result keeps only one attribute of each such pair: Q(A,B,C,D,E)

6 NATURAL JOIN Operation Example
Suppose, we need to display the projects of each department The two relations DEPARTMENT, PROJECT have two attributes DNUMBER, DNUM First we rename the DNUMBER attribute of DEPARTMENT to DNUM, so that it has the same name in both relations. Then apply the natural join

7 Database State for COMPANY
All examples discussed below refer to the COMPANY database shown here.

8 NATURAL JOIN Operation
PROJ_DEPT  Project *  (DNAME, DNUM, MGRSSN, MGRSTARTDATE ) ( DEPARTMENT)

9 NATURAL JOIN Operation
The same query can be done in two steps by creating an intermediate table DEPT DEPT   (DNAME, DNUM, MGRSSN, MGRSTARTDATE ) ( DEPARTMENT) PROJ_DEPT  Project * Dept

10 NATURAL JOIN Operation
If the attributes on which the natural join is already have the same names in both relation then renaming operation is unnecessary For example, to apply a natural join on the DNUMBER attributes of DEPARTMENT and DEPT_LOCATIONS, it is sufficient to write: DEPT_LOCS  DEPARTMENT * DEPT_LOCATIONS

11 DIVISION Operation  Division operation is useful for a special kind of query that sometimes occurs in database applications Assume relation R is defined over the attribute set A , Relation S is defined over the attribute set B such that Let C = A-B , that is C, is the set of attributes of R that are not attributes of S R  S Defines a relation over the attributes C that consists of the set of tuples from R that match the combination of every tuple in S

12 DIVISION Operation  R A B a5 b1 a7 b4 a6 b3 S B b4 b3 R ÷ S A a5

13 DIVISION Operation  R ÷ S

14 Additional Relational Operations
Some common database requests which are needed in commercial query languages for RDBMs cannot be performed with the original relational algebra Additional operations introduced to enhance the expressive power of the original relational algebra

15 Aggregate Functions and Grouping
A type of request that cannot be expressed in the basic relational algebra is to specify mathematical aggregate functions on collections of values from the database. Examples of such functions include retrieving the average or total salary of all employees or the total number of employee tuples. These functions are used in simple statistical queries that summarize information from the database tuples. Common functions applied to collections of numeric values include SUM, AVERAGE, MAXIMUM, and MINIMUM. The COUNT function is used for counting tuples or values.

16 Grouping Group By A R A B 1 5 10 15 2 30 20 50 R A B 1 5 10 15 2 30 20

17 Aggregate Functions We can define an Aggregate function operation using the symbol ℱ (Pronounced Script F) Syntax <Grouping Attributes> ℱ <Function List> (R) Example Retrieve each department number, number of employees in department, and their average salary DNO ℱ Count SSN, Average Salary(Employee)

18 Aggregate Functions ℱ Count SSN, Average Salary(Employee))
DNO ℱ Count SSN, Average Salary(Employee)) ℱ Count SSN, Average Salary(Employee))

19 Aggregate Functions To Rename the output attribute use operator 
 R(DNO, No_Of_Employees, Average_Sal) (DNO ℱ Count SSN, Average Salary(Employee))

20 Aggregate Functions ℱ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]

21 The OUTER JOIN Operation
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 loss some information. A set of operations, called outer joins, can be used when we want to keep all the tuples in R, or all those in S, or all those in both relations in the result of the join, regardless of whether or not they have matching tuples in the other relation.

22 The OUTER JOIN Operation
The left outer join operation keeps every tuple in the first or left relation R in the result of R S; if no matching tuple is found in S, then the attributes of S in the join result are filled 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.

23 R S Natural R S R S R S Left Right Full

24 The OUTER JOIN Operation
List of all employee names and also the names of the departments they manage If they happen to manage a department If they do not manage any indicate with null values Temp  ( Employee Department ) SSN=MGRSSN

25 The OUTER JOIN Operation


Download ppt "The Relational Algebra and Relational Calculus"

Similar presentations


Ads by Google