Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Relational Algebra

Similar presentations


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

1 The Relational Algebra
Database System-dww

2 Chapter Outline Fundamental Operator Deliverable & Additional Operator
Database System-dww

3 What is Relational Algebra?
Relational Algebra is a Procedural Paradigm You Need to Tell What/How to Construct the Result Consists of a Set of Operators Which, When Applied to Relations, Yield Relations (Closed Algebra) Basic Relational Operations: Unary Operations SELECT s PROJECT  or P. Binary Operations Set operations: UNION  INTERSECTION  DIFFERENCE – CARTESIAN PRODUCT  JOIN operations Database System-dww

4 Relational Algebra A1, A2, ..., An (R) projection F (R) selection
R S union R S intersection R \ S set difference R S Cartesian product A1, A2, ..., An (R) projection F (R) selection R S natural join R S theta-join RS division [A1 B1,.., An Bn] rename Database System-dww

5 Selection Selects the Set of Tuples (Rows) From a Relation, Which Satisfy a Selection Condition General Form s<selection condition> (R) R is a Relation Selection condition is a Boolean Expression on the Attributes of R Resulting Relation Has the Same Schema as R Select Finds and Retrieves All Relevant Rows (Tuples) of Table/Relation R which Includes ALL of its Columns (Attributes) Database System-dww

6 Selection Example TITLE='Elect. Eng.'(EMP)
ENO ENAME TITLE E1 J. Doe Elect. Eng. E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu E7 R. Davis E8 J. Jones EMP ENO ENAME TITLE E1 J. Doe Elect. Eng E6 L. Chu Elect. Eng. TITLE='Elect. Eng.'(EMP) TITLE='Elect. Eng.’ OR TITLE=‘Mech.Eng’(EMP) Database System-dww

7 Another Selection Example
null W B Database System-dww

8 Selection Condition A SELECT Condition is a Boolean Expression
Form F1 Y F2 Y ..., Y Fq (Q>=1), Where Fi (I=1,…,q) are Atomic Boolean Expressions of the Form aqc or aqb, a, b are Attributes of R and c is a Constant. The Operator q is one of the Arithmetic Comparison Operators: <, >, =, <>, >=, <= The Operator Y is one of the Logical Operators: , , ¬ Nesting: ( ) Database System-dww

9 Projection Select certain Columns (Attributes) Specified in an Attribute List X From a Relation R General Form <attribute-list>(R) R is a Relation Attribute-list is a Subset of the Attributes of R Over Which the Projection is Performed Project Retrieves Specified Columns of Table/Relation R which Includes ALL of its Rows (Tuples) Database System-dww

10 Projection Example  PNO,BUDGET(PROJ) PROJ PNO BUDGET P2 135000 P3
250000 P4 310000 P5 500000 PNAME P1 150000 Instrumentation Database Develop. CAD/CAM Maintenance  PNO,BUDGET(PROJ) PNO BUDGET P1 150000 P2 135000 P3 250000 P4 310000 P5 500000 Database System-dww

11 Other Projection Examples
Database System-dww

12 Relational Algebra Expression
Several Operations can be Combined to form a Relational Algebra Expression (query) Example: Retrieve all Customers over age 60? Method 1:  CNAME, ADDRESS, AGE (s AGE>60(CUSTOMER) ) Method 2: Senior-CUST(C#, Addr, Age) =  CNAME, ADDRESS, AGE (s AGE>60(CUSTOMER) ) Method 3:  CNAME, ADDRESS, AGE (C) where C = s AGE>60(CUSTOMER) Database System-dww

13 Characteristics of Projection
The PROJECT Operation Eliminates Duplicate Tuples in the Resulting Relation Why? Projection Must Maintain a Mathematical Set (No Duplicate Elements) ENO ENAME TITLE E1 J. Doe Elect. Eng. E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu E7 R. Davis E8 J. Jones EMP  TITLE(PROJ) Elect.Eng Syst.Anal Mech.Eng Programmer TITLE Database System-dww

14 Selection with Projection Example
Database System-dww

15 Renaming The RENAME operator gives a new schema to a relation.
R1 := RENAMER1(A1,…,An)(R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2. Simplified notation: R1(A1,…,An) := R2. Other use () notation R2  R1(A1,…,An) Database System-dww

16 Sequences of Assignments
Create temporary relation names. Renaming can be implied by giving relations a list of attributes. Example: R3 := R1 JOINC R2 can be written: R4 := R1 * R2 R3 := SELECTC (R4) OR R4  R1 * R2 R3  SELECTC (R4) Database System-dww

17 Union General Form R  S where R, S are Relations Example:
Result contains Tuples from both R and S Duplications are Removed The two Operands R, S should be union-compatible Example: “find students registered for course C1 or C3” s#(CNO=‘C1’ (S-C)) s#(CNO=‘C3’ (S-C)) Database System-dww

18 Union Compatibility Two Relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are said Union-compatible If and Only If They Have The Same Number of Attributes The Domains of Corresponding Attributes are Compatible, i.e., Dom(Ai)=dom(Bi) for i=1, 2, ..., N Names Do Not Have to be Same! For Relational Union and Difference Operations, the Operand Relations Must Be Union Compatible The Resulting Relation for Relational Set Operations Has the Same Attribute Names as the First Operand Relation R1 (by Convention) Database System-dww

19 Set Difference General Form R – S where R and S are Relations Example
Result Contains all tuples that are in R, but not in S. R – S <> S – R Again, there Must be Compatibility Example “Find the students who registered course C1 but not C3” s#(CNO=‘C1’ (S-C)) –s#(CNO=‘C3’ (S-C)) Database System-dww

20 Set Intersection s#(CNO=‘C1’ (S-C)) s#(CNO=‘C3’ (S-C))
General Form R S where R and S are Relations Result Contains all Tuples that are in R and S. R S = R – (R – S) Again, there Must be Compatibility Example “find the students who registered for both C1 and C3” s#(CNO=‘C1’ (S-C)) s#(CNO=‘C3’ (S-C)) Database System-dww

21 Union, Difference, Intersection Examples
What are these Other Three Result Tables? STUDENT  INSTRUCTOR STUDENT - INSTRUCTOR Database System-dww INSTRUCTOR - STUDENT

22 Cartesian Product Given Relations Cartesian Product R  S
R of Degree k1 and Cardinality card1 S of Degree k2 and Cardinality card2 Cartesian Product R  S is a Relation of Degree (k1+ k2) and Consists of Tuples of Degree (k1+ k2) where each Tuple is a Concatenation of one Tuple of R with one Tuple of S Cardinality of the Result of the Cartesian Product R  S is card1 * card2 What is One Problem with Cartesian Product w.r.t. the Result Set? Database System-dww

23 Cartesian Product: Example
B C E F a1 a2 a3 b1 b1b4 c3 c5 c7 e1 e2 f1 f5 A B C E F R S a1 a2 a3 b1 b4 c3 c5 c7 e1 e2 f1 f5 Database System-dww

24 Cartesian Product Given R(A1, …,An) and S(B1,…,Bm), the result of a Cartesian product R  S is a relation of schema R’(A1, …, An, B1, …, Bm). Example “Get a list containing (S#, C#) for all students who live in Storrs but are not registered for the database course” (S#(city=‘Storrs’(STUDENT))   C# (CNAME=‘Database’(COURSE))) – S#, C#(S-C) Database System-dww

25 Cartesian Product Example
ENO ENAME TITLE E1 J. Doe Elect. Eng E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu Elect. Eng. E7 R. Davis E8 J. Jones EMP ENO ENAME EMP.TITLE SAL.TITLE SAL E1 J. Doe Elect. Eng. 40000 Syst. Anal. 34000 Mech. Eng. 27000 Programmer 24000 E2 M. Smith E3 A. Lee E8 J. Jones EMP SAL TITLE SAL Elect. Eng. 40000 Syst. Anal. 34000 Mech. Eng. 27000 Programmer 24000 Database System-dww

26 The Join Operation Used to combine related tuples from two relation into single tuples with some condition Cartesian product all combination of tuples are included in the result, meanwhile in the join operation, only combination of tuples satisfying the join condition appear in the result Database System-dww

27 Theta Join (-Join) General Form R  S where
R, S are Relations,  is a Boolean Expression, called a Join Condition. A Derivative of Cartesian Product R  S =  (R  S) R(A1, A2, ..., Am, B1, B2, ..., Bn) is the Resulting Schema of a -Join over R1 and R2: R1(A1, A2, ..., Am)  R2 (B1, B2, ..., Bn) -Join is derived from a Cartesian Product followed by a SELECT. Database System-dww

28 -Join Condition A -Join Condition is a Boolean Expression of the form F1 y1 F2 y2 ..., yn-1 Fq (q>=1), where Fi (i=1,…,q) are Atomic Boolean Expressions of the form Ai  Bj, Ai, Bj are Attributes of R1 and R2 Respectively q is one of the Algorithmic Comparison Operators =, <>, >, <. >=, <= The Operator yi (i=1,…,n-1) is Either a Logical AND operator  or a logical OR operator  Database System-dww

29 -Join Example EMP E.TITLE=SAL.TITLE SAL EMP SAL 40000 34000 27000
ENO ENAME TITLE E1 J. Doe Elect. Eng E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu Elect. Eng. E7 R. Davis E8 J. Jones EMP EMP E.TITLE=SAL.TITLE SAL SAL 40000 34000 27000 24000 ENO ENAME TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. SAL.TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. E1 J. Doe E2 M. Smith E3 A. Lee TITLE SAL Elect. Eng. 40000 Syst. Anal. 34000 Mech. Eng. 27000 Programmer 24000 E4 J. Miller E5 B. Casey E6 L. Chu E7 R. Davis E8 J. Jones Database System-dww

30 Other Types of Join Equi-join (EQUIJOIN) Natural Join
The  Expression only Contains one or more Equality Comparisons Involving Attributes from R1 and R2 Natural Join Denoted as R S Special Equi-join of Two Relations R and S Over a Set of Attributes Common to both R and S By Common, it means that each Join Attribute in A has not only Compatible Domains but also the Same Name in both Relations R and S Equi-join (EQUIJOIN) The formula F only contains one or more equality comparisons involving attributes from R1 and R2. Natural join Equi-join of two relations R and S over a set of attributes (say A) common to both R and S denoted as R A S. Difference In an EQUIJOIN, the join attribute of S appear redundantly in the result relation. In a NATURAL JOIN, the redundant join attributes of S are eliminated from the result relation. The equality condition is implied. Database System-dww

31 Examples R S A B C B E a1 a2 a3 b1 b1b4 c3 c5 c7 b1 b5 e1 e2
R R.B=S.B S A R.B a1 a2 b1 C E c3 c5 e1 S.B EQUIJOIN R S Natural Join Database System-dww

32 Natural Join Natural Join Combines Relations on Attributes with the Same Names STUDENT(S#, SN, CITY, ) SC(S#, C#, G) Example Query 1: “list of students with complete course grade info” STUDENT SC Database System-dww

33 Natural Join All Natural Joins can be Expressed by a Combination of Primitive Operators Example Query 2: “print all students info (courses taken and grades)” Database System-dww

34 Natural Join Example EMP EMP SAL Database System-dww ENO ENAME TITLE
J. Doe Elect. Eng E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu Elect. Eng. E7 R. Davis E8 J. Jones EMP EMP SAL ENO ENAME E.TITLE SAL E1 J. Doe Elect. Eng. 70000 E2 M. Smith Syst. Anal. 80000 E3 A. Lee Mech. Eng. 56000 E4 J. Miller Programmer 60000 E5 B.Casey Syst.Anal 80000 TITLE SAL Elect. Eng. 70000 Syst. Anal. 80000 Mech. Eng. 56000 Programmer 60000 E6 L. Chu Elect.Eng 70000 E7 R.Davis Mech.Eng 56000 E8 J. Jones Syst. Anal. 80000 Database System-dww

35 Database System-dww

36 Another Join Example Database System-dww

37 Quotient (Division) Given Relations The Division of R by S, R ÷ S
R(T,U) of degree r S(U) of degree s The Division of R by S, R ÷ S Results is a Relation of Degree (rs) Consists of all (rs)-tuples t such that for all s-tuples u in S, the tuple tu is in R. Database System-dww

38 Division Example ENO PNO PNAME E1 P1 Instrumentation 150000 BUDGET E2 P2 Database Develop. 135000 E3 P4 Maintenance E4 E5 E6 E7 P3 CAD/CAM E8 310000 250000 R Find the employees who work for both project P1 and project P4? S PNO PNAME BUDGET P1 Instrumentation 150000 P4 Maintenance 310000 R ÷ S ENO E3 Database System-dww

39 Database System-dww

40 Database System-dww

41 Division: Another Example
“list the S# of students that have taken all of the courses listed in SC” S# (SCC# (SC)) “list the S# of students who have taken all of the courses taught by instructor Smith” S# (SCC# (Instructor=‘Smith’(SC))) Database System-dww

42 Relational Algebra Fundamental Operators
Selection Projection Union Difference Cartesian Product Intersection Join, Equi-join, Natural Join Quotient (Division) Fundamental Operators Derivable from the fundamental operators Database System-dww

43 All Relational Algebra Operations
A Set of Relational Algebra Operations Is Called a Complete Set, If and Only If Any Relational Algebra Operator in the Set Cannot be Derived in Terms of a Sequence of Others in Set Any Relational Algebra Operator Not in the Set Can Be Derived in Terms of a Sequence of Only the Operators in the Set All the operations discussed so far (Join, Natural Join) can be described as a sequence of only the operations SELECT, PROJECT , UNION, SET DIFFERENCE, and CARTESIAN PRODUCT. Hence, the set {s ,P , U, - , X } is called a complete set of relational algebra operations. Any query language equivalent to these operations is called relationally complete. Database System-dww

44 All Relational Algebra Operations
Important Concepts: The Set of Algebra Operations {, P , , –, } is a Complete Set of Relational Algebra Operations Any Query Language Equivalent to These Five Operations is Called Relationally Complete All the operations discussed so far (Join, Natural Join) can be described as a sequence of only the operations SELECT, PROJECT , UNION, SET DIFFERENCE, and CARTESIAN PRODUCT. Hence, the set {s ,P , U, - , X } is called a complete set of relational algebra operations. Any query language equivalent to these operations is called relationally complete. Database System-dww

45 Additional Relational Operations
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. Database System-dww

46 Additional Relational Operations (cont.)
Database System-dww

47 Additional Relational Operations (cont.)
Use of the Functional operator ℱ ℱ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] Database System-dww

48 Database System-dww

49 Database System-dww

50 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. Example: 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. Database System-dww

51 Additional Relational Operations (cont.)
Database System-dww

52 Additional Relational Operations (cont.)
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 amounts to loss of 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. 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 filled or “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. Database System-dww

53 Additional Relational Operations (cont.)
TEMP  (EMPLOYEE SSN=MGRSSN DEPARTMENT RESULT  P FNAME, MINIT, LNAME, DNAME (TEMP) Database System-dww

54 Additional Relational Operations (cont.)
OUTER UNION Operations The outer union operation was developed to take the union of tuples from two relations if the relations are not union compatible. This operation will take the union of tuples in two relations R(X, Y) and S(X, Z) that are partially compatible, meaning that only some of their attributes, say X, are union compatible. The attributes that are union compatible are represented only once in the result, and those attributes that are not union compatible from either relation are also kept in the result relation T(X, Y, Z). Database System-dww

55 Database System-dww

56 Database System-dww

57 Additional Relational Operations (cont.)
OUTER UNION Operations Example: An outer union can be applied to two relations whose schemas are STUDENT(Name, SSN, Department, Advisor) and INSTRUCTOR(Name, SSN, Department, Rank). Tuples from the two relations are matched based on having the same combination of values of the shared attributes—Name, SSN, Department. If a student is also an instructor, both Advisor and Rank will have a value; otherwise, one of these two attributes will be null. The result relation STUDENT_OR_INSTRUCTOR will have the following attributes: STUDENT_OR_INSTRUCTOR (Name, SSN, Department, Advisor, Rank) Database System-dww

58 Database System-dww

59 Q1: Retrieve the name and address of all employees who work for the ‘Research’ department.
RESEARCH_DEPT   DNAME=’Research’ (DEPARTMENT) RESEARCH_EMPS  (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE) RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS) This query could be specified in other ways; for example, the order of the JOIN and SELECT could be reversed, or the JOIN could be replaced by a NATURAL JOIN after renaming one of the join attributes. RESULT   FNAME, LNAME, ADDRESS ( DNAME=’Research’ (DEPARTMENT) DNUMBER= DNOEMPLOYEE)) Database System-dww

60 Q2: 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) RESULT   LNAME, FNAME (( SSN(EMPLOYEE) -  ESSN(DEPENDENT)) * EMPLOYEE) Database System-dww

61 Q3: List the name of all employees with two or more dependents
T1 (SSN, NO_OF_DEPT)  ESSN ℱ COUNT DEPENDENT_NAME (DEPENDENT) T2   NO_OF_DEPS >= 2 (T1) RESULT   LNAME, FNAME (T2 * EMPLOYEE) Database System-dww

62 Exercises Database System-dww

63 From the above schema, (a) Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project. (b) List the names of employees who have a dependent with the same first name as themselves. (c) Find the names of employees that are directly supervised by 'Franklin Wong'.  (d) For each project, list the project name and the total hours per week (by all employees) spent on that project. (e) Retrieve the names of employees who work on every project. Database System-dww

64 Exercises (f) Retrieve the names of employees who do not work on any project. (g) For each department, retrieve the department name, and the average salary of employees working in that department. (h) Retrieve the average salary of all female employees. (i) Find the names and addresses of employees who work on at least one project located in Houston but whose department has no location in Houston. List the last names of department managers who have no dependents. Database System-dww

65 Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project. EMPLOYEE_WORKS_ON_ProductX  (PNAME=‘ProductX’(PROJECT) PNUMBER=PNO(WORKS_ON)) EMPLOYEE_WORKS_MORE_10  ((EMPLOYEE) SSN=ESSN( HOURS>10(EMPLOYEE_WORKS_ON_ProductX))) RESULT  FNAME,LNAME,ADDRESS( EMPLOYEE_WORKS_MORE_10) Database System-dww

66 List the names of employees who have a dependent with the same first name as themselves
EMPLOYEE_FNAME_SAME_DEPENDENT_NAME  ((EMPLOYEE) (SSN,FNAME)=(ESSN,DEPENDENT_NAME)(DEPENDENT)) RESULT  FNAME,LNAME(EMPLOYEE_FNAME_SAME_DEPENDENT_NAME) Database System-dww


Download ppt "The Relational Algebra"

Similar presentations


Ads by Google