Download presentation
Presentation is loading. Please wait.
1
Relational Algebra Agenda: - Mathematical basis of Data Manipulation - Relational Algebra operators
2
Data Manipulation Language (a) All data in a relational DB is stored in tables (b) A normalized DB has many tables (c) Parts of some information we need is located in different tables Example (E-D-P db): Which projects does the Manager of Research Dept work on? DML: A computer-language to specify - what data we want to access, and - what we want to do to the data. Relational Algebra can help to understand SQL (standard DML).
3
Relational Algebra (RA) What is an algebra ? Formal mathematical system to manipulate symbols Common useful algebras: Real algebra, Complex algebra, Boolean algebra, … RA: manipulate (add, multiply, …) schema instances - schema instance = = table = = set of tuples
4
RA: Learning by examples.. NameIDSupervisorIDDeptNo John1112225 Frankie2227774 Alice3334444 Jennifer4447774 William5552225 Joyce6662225 James7772225 John888null1 IDnoProjNoHours 11112.5 11121.5 55532.5 66613.5 66623.5 22222.5 22235.5 22245.5 22251.5 EmpIDDepNameRelationship 222JackSon 222JillWife 222JohnSon 444TedSon 111MikeSon 111AnitaDaughter DEPENDENT EMPLOYEE WORKS_ON
5
RA Operators: SELECT USE: to select a subset of the tuples in a table SYNTAX: SELECT [conditions] ( TABLE ) Example: OUTPUT = SELECT [DeptNo = 5] ( EMPLOYEE ) NameIDSupervisorIDDeptNo John1112225 Frankie2227774 Alice3334444 Jennifer4447774 William5552225 Joyce6662225 James7772225 John888null1 EMPLOYEE OUTPUT NameIDSupervisorIDDeptNo John1112225 William5552225 Joyce6662225 James7772225
6
RA Operators: SELECT.. SYNTAX: SELECT [conditions] ( TABLE ) Example: OUTPUT = SELECT [ (DeptNo != 4) AND ( (ID = 222) OR (ID = 111)) ] ( EMPLOYEE) NameIDSupervisorIDDeptNo John1112225 Frankie2227774 Alice3334444 Jennifer4447774 William5552225 Joyce6662225 James7772225 John888null1 EMPLOYEE OUTPUT NameIDSupervisorIDDeptNo John1112225 conditions - can be any logical expression - must be evaluated for each tuple independently
7
RA Operators: PROJECT USE: to select a subset of the columns in a table SYNTAX: PROJECT [attributes] ( TABLE ) Example: OUTPUT = PROJECT [ Name, ID] ( EMPLOYEE ) NameIDSupervisorIDDeptNo John1112225 Frankie2227774 Alice3334444 Jennifer4447774 William5552225 Joyce6662225 James7772225 John888null1 EMPLOYEE NameID John111 Frankie222 Alice333 Jennifer444 William555 Joyce666 James777 John888 EMPLOYEE
8
RA Operators: PROJECT.. Notes: - PROJECT returns a table - PROJECT returns a set of tuples (no repeated elements) SYNTAX: PROJECT [attributes] ( TABLE ) Example: OUTPUT = PROJECT [EmpID, Relationship] ( DEPENDENT ) EmpIDDepNameRelationship 222JackSon 222JillWife 222JohnSon 444TedSon 111MikeSon 111AnitaDaughter DEPENDENT EmpIDRelationship 222Son 222Wife 444Son 111Son 111Daughter OUTPUT
9
Combination of RA Operators Combinations are equivalent to sequence of operations Arbitrary combinations of RA operators are allowed Example: OUTPUT = PROJECT [Name, ID] ( SELECT [ SupervisorID = 222] ( EMPLOYEE) ) Equivalent to: OUTPUT1 = SELECT [ SupervisorID = 222] ( EMPLOYEE) OUTPUT = PROJECT [Name, ID] ( OUTPUT1) Why ? NameIDSupervisorIDDeptNo John1112225 Frankie2227774 Alice3334444 Jennifer4447774 William5552225 Joyce6662225 James7772225 John888null1 EMPLOYEE OUTPUT NameID John111 William555 Joyce666 James777
10
RA Operators: JOIN USE: to combine data from two tables into one SYNTAX: JOIN [conditions] ( TABLE1, TABLE2 ) Example: OUTPUT = JOIN [ID = IDno] ( EMPLOYEE, WORKS_ON ) NameIDSupervisorIDDeptNo John1112225 Frankie2227774 Alice3334444 Jennifer4447774 William5552225 Joyce6662225 James7772225 John888null1 EMPLOYEE IDnoProjNoHours 11112.5 11121.5 55532.5 66613.5 66623.5 22222.5 22235.5 22245.5 22251.5 WORKS_ON …
11
OUTPUT NameIDSupervisorIDDeptNoIDnoProjNoHours John111222511112.5 John111222511121.5 Frankie222777422222.5 Frankie222777422235.5 Frankie222777422245.5 Frankie222777422251.5 William555222555532.5 Joyce666222566613.5 Joyce666222566623.5 RA Operators: JOIN.. USE: to combine data from two tables into one SYNTAX: JOIN [conditions] ( TABLE1, TABLE2 ) Example: OUTPUT = JOIN [ID = IDno] ( EMPLOYEE, WORKS_ON )
12
RA Operators: JOIN How JOIN works NameIDSupervisorIDDeptNo John1112225 Frankie2227774 Alice3334444 Jennifer4447774 William5552225 Joyce6662225 James7772225 John888null1 EMPLOYEE IDnoProjNoHours 11112.5 11121.5 55532.5 66613.5 66623.5 22222.5 22235.5 22245.5 22251.5 WORKS_ON 1. Cartesian Product of the two tables is formed 2. For each tuple in the Cartesian Product, condition is tested if TRUE the tuple is placed in OUTPUT else the tuple is discarded
13
RA Operators: OUTER JOINS USE: Join operations where we want to preserve at least one instance of each tuple (of a table) NameIDSupervisorIDDeptNo John1112225 Frankie2227774 Alice3334444 Jennifer4447774 William5552225 Joyce6662225 James7772225 John888null1 EMPLOYEE Example: OUTPUT = LEFT-OUTER-JOIN [ID = EmpID] ( EMPLOYEE, DEPENDENT ) EmpIDDepNameRelationship 222JackSon 222JillWife 222JohnSon 444TedSon 111MikeSon 111AnitaDaughter DEPENDENT tuples in red have no match in DEPENDENT
14
RA Operators: OUTER JOINS USE: Join operations where we want to preserve at least one instance of each tuple (of a table) Example: OUTPUT = LEFT-OUTER-JOIN [ID = EmpID] ( EMPLOYEE, DEPENDENT ) OUTPUT NameIDSupervisorIDDeptNoEmpIDDepNameRelationship John1112225111MikeSon John1112225111AnitaDaughter Frankie2227774222JackSon Frankie2227774222JillWife Frankie2227774222JohnSon Alice3334444null Jennifer4447774444TedSon William5552225null Joyce6662225null James7772225null John888null1
15
RA – Set theoretic operators: UNION USE: Union of two instances of similarly defined schemas SYNTAX:UNION ( TABLE1, TABLE2) Example: X = UNION((SELECT [EmpID = 222] (DEPENDENTS)), ( SELECT [EmpID = 444] ( DEPENDENTS))) X EmpIDDepNameRelationship 222JackSon 222JillWife 222JohnSon 444TedSon EmpIDDepNameRelationship 222JackSon 222JillWife 222JohnSon 444TedSon 111MikeSon 111AnitaDaughter DEPENDENT
16
RA – Set theoretic operators: INTERSECTION USE: Intersection of two instances of similarly defined schemas SYNTAX:INTERSECTION ( TABLE1, TABLE2) Example: X = INTERSECTION (( (SELECT [EmpID = 222] (DEPENDENTS)), ( SELECT [Relationship = SON] ( DEPENDENTS))) X EmpIDDepNameRelationship 222JackSon 222JohnSon EmpIDDepNameRelationship 222JackSon 222JillWife 222JohnSon 444TedSon 111MikeSon 111AnitaDaughter DEPENDENT
17
RA – Set theoretic operators: DIFFERENCE USE: Set difference of two similarly defined schemas Example: Y = DIFFERENCE ( SELECT [EmpID = 222] (DEPENDENTS), SELECT [Relationship = SON] ( DEPENDENTS)) Y EmpIDDepNameRelationship 222JillWife EmpIDDepNameRelationship 222JackSon 222JillWife 222JohnSon 444TedSon 111MikeSon 111AnitaDaughter DEPENDENT Note: DIFFERENCE is not commutative. SYNTAX:DIFFERENCE ( A, B)
18
RA – Set theoretic operators: DIVIDEBY USE: Reports the quotient of dividing one table by another Example: OUTPUT = DIVIDEBY( WORKSON, PROJ) Syntax:DIVIDEBY( A, B) WORKS_ON EmployeeIDProjectNo 1111 2 2222 3 1 3332 PROJECTS ProjectNo 1 2 3 = OUTPUT EmployeeID 222 Note: Useful in answering “for-all” type of queries
19
Summary Relational Algebra is an elegant mathematical DML Practice: Industry standard DML is Structured Query Language (SQL) Mathematical basis for SQL: Relational Calculus Relational Calculus is functionally equivalent to RA Next topic: SQL
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.