260 likes | 470 Views
Relational Algebra. Agenda: - Mathematical basis of Data Manipulation - Relational Algebra operators. 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
E N D
Relational Algebra Agenda: - Mathematical basis of Data Manipulation - Relational Algebra operators
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).
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
RA: Learning by examples.. WORKS_ON EMPLOYEE DEPENDENT
RA Operators: SELECT USE: to select a subset of the tuples in a table SYNTAX: SELECT [conditions] ( TABLE ) Example: OUTPUT = SELECT [DeptNo = 5] ( EMPLOYEE ) EMPLOYEE
RA Operators: SELECT.. SYNTAX: SELECT [conditions] ( TABLE ) conditions - can be any logical expression - must be evaluated for each tuple independently Example: OUTPUT = SELECT [ (DeptNo != 4) AND ( (ID = 222) OR (ID = 111)) ] ( EMPLOYEE) EMPLOYEE
RA Operators: PROJECT USE: to select a subset of the columns in a table SYNTAX: PROJECT [attributes] ( TABLE ) Example: OUTPUT = PROJECT [ Name, ID] ( EMPLOYEE ) EMPLOYEE EMPLOYEE
RA Operators: PROJECT.. SYNTAX: PROJECT [attributes] ( TABLE ) Notes: - PROJECT returns a table - PROJECT returns a set of tuples (no repeated elements) Example: OUTPUT = PROJECT [EmpID, Relationship] ( DEPENDENT ) DEPENDENT OUTPUT
Combination of RA Operators Why ? Arbitrary combinations of RA operators are allowed Combinations are equivalent to sequence of operations Example: OUTPUT = PROJECT [Name, ID] ( SELECT [ SupervisorID = 222] ( EMPLOYEE) ) Equivalent to: OUTPUT1 =SELECT [ SupervisorID = 222] ( EMPLOYEE) OUTPUT = PROJECT [Name, ID] ( OUTPUT1) EMPLOYEE
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 ) WORKS_ON EMPLOYEE …
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 )
RA Operators: JOIN How JOIN works 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 WORKS_ON EMPLOYEE
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 ) DEPENDENT EMPLOYEE tuples in red have no match in DEPENDENT
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 )
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))) DEPENDENT
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))) DEPENDENT
RA – Set theoretic operators: DIFFERENCE USE: Set difference of two similarly defined schemas SYNTAX: DIFFERENCE ( A, B) Example: Y = DIFFERENCE ( SELECT [EmpID = 222] (DEPENDENTS), SELECT [Relationship = SON] ( DEPENDENTS)) DEPENDENT Note: DIFFERENCE is not commutative.
RA – Set theoretic operators: DIVIDEBY USE: Reports the quotient of dividing one table by another Syntax: DIVIDEBY( A, B) Example: OUTPUT = DIVIDEBY( WORKSON, PROJ) = Note: Useful in answering “for-all” type of queries
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