# Relational Algebra Indra Budi Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT 

## Presentation on theme: "Relational Algebra Indra Budi Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT "— Presentation transcript:

Relational Algebra Indra Budi indra@cs.ui.ac.id

Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT  or . l Binary Operations à Set operations:  UNION   INTERSECTION  F DIFFERENCE –  CARTESIAN PRODUCT  à JOIN operations  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)

Fakultas Ilmu Komputer UI 3 Relational Algebra R  Sunion R  Sintersection R \ Sset difference R  SCartesian product  A1, A2,..., An (R)projection  F (R)selection R Snatural join R   Stheta-join R  Sdivision  [ A 1 B 1,.., An Bn ]rename

Fakultas Ilmu Komputer UI 4 Selection Selects the Set of Tuples (Rows) From a Relation, Which Satisfy a Selection Condition General Form  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)

Fakultas Ilmu Komputer UI 5 ENOENAMETITLE E1J. DoeElect. Eng E6L. ChuElect. Eng.  TITLE='Elect. Eng.' (EMP) ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP  TITLE='Elect. Eng.’ OR TITLE=‘Mech.Eng’ (EMP) Selection Example

Fakultas Ilmu Komputer UI 6 Another Selection Example A S C null W B null

Fakultas Ilmu Komputer UI 7 A SELECT Condition is a Boolean Expression Form F 1  F 2  F q (Q>=1), Where F i (I=1,…,q) are Atomic Boolean Expressions of the Form a  c or a  b, a, b are Attributes of R and c is a Constant. The Operator  is one of the Arithmetic Comparison Operators :, =, <>  The Operator  is one of the Logical Operators: , , ¬ Nesting: ( ) Selection Condition

Fakultas Ilmu Komputer UI 8 Select certain Columns (Attributes) Specified in an Attribute List X From a Relation R General Form  ( 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) Projection

Fakultas Ilmu Komputer UI 9 Projection Example  PNO,BUDGET (PROJ) PNOBUDGET P1150000 P2135000 P3250000 P4310000 P5500000 PROJ PNOBUDGET P2135000 P3250000 P4310000 P5500000 PNAME P1150000Instrumentation Database Develop. CAD/CAM Maintenance CAD/CAM

Fakultas Ilmu Komputer UI 10 Other Projection Examples

Fakultas Ilmu Komputer UI 11 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 (  AGE>60 (CUSTOMER) ) Method 2: Senior-CUST(C#, Addr, Age) =  CNAME, ADDRESS, AGE (  AGE>60 (CUSTOMER) ) Method 3:  CNAME, ADDRESS, AGE (C) where  C  AGE>60 (CUSTOMER)

Fakultas Ilmu Komputer UI 12 ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP  TITLE (PROJ) Elect.Eng Syst.Anal Mec.Eng Programmer TITLE Characteristics of Projection The PROJECT Operation Eliminates Duplicate Tuples in the Resulting Relation Why? Projection Must Maintain a Mathematical Set (No Duplicate Elements)

Fakultas Ilmu Komputer UI 13 Selection with Projection Example

Fakultas Ilmu Komputer UI 14 Renaming The RENAME operator gives a new schema to a relation. R1 := RENAME R1(A1,…,A n ) (R2) makes R1 be a relation with attributes A1,…,A n and the same tuples as R2. Simplified notation: R1(A1,…,A n ) := R2. Other use (  ) notation R2  R1(A1,…,A n )

Fakultas Ilmu Komputer UI 15 Sequences of Assignments Create temporary relation names. Renaming can be implied by giving relations a list of attributes. Example: R3 := R1 JOIN C R2 can be written: R4 := R1 * R2 R3 := SELECT C (R4) OR R4  R1 * R2 R3  SELECT C (R4)

Fakultas Ilmu Komputer UI 16 General Form R  SR  S where R, S are Relations 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)) Union

Fakultas Ilmu Komputer UI 17 Union Compatibility Two Relations R 1 (A 1, A 2,..., A n ) and R 2 (B 1, B 2,..., B n ) 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(A i )=dom(B i ) 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 R 1 (by Convention)

Fakultas Ilmu Komputer UI 18 General Form R – S where R and S are Relations 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)) Set Difference

Fakultas Ilmu Komputer UI 19 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)) Set Intersection

Fakultas Ilmu Komputer UI 20 Union, Difference, Intersection Examples What are these Other Three Result Tables?

Fakultas Ilmu Komputer UI 21 Given Relations R of Degree k 1 and Cardinality card 1 S of Degree k 2 and Cardinality card 2 Cartesian Product R  S R  S is a Relation of Degree ( k 1 + k 2 ) and Consists of Tuples of Degree ( k 1 + k 2 ) 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 card 1 * card 2 What is One Problem with Cartesian Product w.r.t. the Result Set? Cartesian Product

Fakultas Ilmu Komputer UI 22 Cartesian Product: Example ABC a1 a2 a3 b1 b1 b4 c3 c5 c7 F E f1 f5 e1 e2 ABCE a1 a2 a3 b1 b4 c3 c5 c7 e1 e2 e1 e2 e1 e2 RS R S F f1 f5 f1 f5 f1 f5 

Fakultas Ilmu Komputer UI 23 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 ” Cartesian Product (  S# (  city=‘Storrs’ ( STUDENT ))    C# (  CNAME=‘Database’ ( COURSE )))  –  S#, C# ( S-C )

Fakultas Ilmu Komputer UI 24 Cartesian Product Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng.40000 Syst. Anal.34000 Mech. Eng.27000 Programmer24000

Fakultas Ilmu Komputer UI 25 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

Fakultas Ilmu Komputer UI 26 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(A 1, A 2,..., A m, B 1, B 2,..., B n ) is the Resulting Schema of a  -Join over R 1 and R 2 : R 1 (A 1, A 2,..., A m )  R 2 (B 1, B 2,..., B n ) Theta Join (  -Join)

Fakultas Ilmu Komputer UI 27 A  -Join Condition is a Boolean Expression of the form F 1  1  F 2  2  n-1  F q (q>=1), where F i (i=1,…,q) are Atomic Boolean Expressions of the form A i  B j, A i, B j are Attributes of R 1 and R 2 Respectively  is one of the Algorithmic Comparison Operators =, <>, >, =, <= The Operator  i ( i=1,…,n-1 ) is Either a Logical AND operator  or a logical OR operator   -Join Condition

Fakultas Ilmu Komputer UI 28  -Join Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng. 40000 Syst. Anal.34000 Mech. Eng.27000 Programmer24000 ENOENAME E1J. Doe M. SmithE2 E3A. Lee E4J. Miller E5B. Casey E6L. Chu E7R. Davis E8J. Jones TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. Elect. Eng. Mech. Eng. Syst. Anal. SAL 40000 34000 27000 24000 34000 40000 27000 34000 EMP E.TITLE=SAL.TITLE SAL SAL.TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. Elect. Eng. Mech. Eng. Syst. Anal.

Fakultas Ilmu Komputer UI 29 Other Types of Join Equi-join (EQUIJOIN) The  Expression only Contains one or more Equality Comparisons Involving Attributes from R 1 and R 2 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

Fakultas Ilmu Komputer UI 30 Examples ABC a1 a2 a3 b1 b1 b4 c3 c5 c7 BE b1 b5 e1 e2 RS R R.B=S.B S AR.B a1 a2 b1 CE c3 c5 e1 S.B b1 EQUIJOIN AR.BCE a1 a2 b1 c3 c5 e1 R S Natural Join

Fakultas Ilmu Komputer UI 31 Natural Join Natural Join Combines Relations on Attributes with the Same Names STUDENT(S#, SN, CITY, Email) S-C(S#, C#, G) Example Query 1: “ list of students with complete course grade info ” STUDENT S-C All Natural Joins can be Expressed by a Combination of Primitive Operators Example Query 2: “ print all students info (courses taken and grades) ”  S#, SN, CITY, Email, C#, G (  STUDENT.S# = S-C.S# (STUDENT  S-C))

Fakultas Ilmu Komputer UI 32 Natural Join Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng.70000 Syst. Anal.80000 Mech. Eng.56000 Programmer60000 ENOENAMEE.TITLE SAL E1J. DoeElect. Eng. 70000 E2M. SmithSyst. Anal. 80000 56000 80000 E3A. LeeMech. Eng. E8J. JonesSyst. Anal. EMP SAL 60000 E4J. MillerProgrammer 80000 E5B.CaseySyst.Anal 70000 E6L. ChuElect.Eng 56000 E7R.DavisMech.Eng

Fakultas Ilmu Komputer UI 33 Another Natural Join Example

Fakultas Ilmu Komputer UI 34 Yet Another Natural Join Example 14551455

Fakultas Ilmu Komputer UI 35 Given Relations 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. Quotient (Division)

Fakultas Ilmu Komputer UI 36 Division Example ENO E3 R ÷ S ENOPNOPNAME E1P1Instrumentation 150000 BUDGET E2P1Instrumentation 150000 E2P2Database Develop.135000 E3P1Instrumentation E3P4Maintenance E4P2Instrumentation E5P2Instrumentation E6P4 E7P3CAD/CAM E8P3CAD/CAM 310000 150000 310000 250000 R Maintenance 150000 S PNO PNAME BUDGET P1 Instrumentation150000 P4Maintenance310000 Find the employees who work for both project P1 and project P4?

Fakultas Ilmu Komputer UI 37 Division: Another Example “ list the S# of students that have taken all of the courses listed in S-C ”  S# (S-C)   C# (S-C) “ list the S# of students who have taken all of the courses taught by instructor Smith ”  S# (S-C   C# (  Instructor=‘Smith’ (C)))

Fakultas Ilmu Komputer UI 38 Relational Algebra Fundamental OperatorsDerivable from the fundamental operators Selection Projection Union Difference Cartesian Product n Intersection n Join, Equi-join, Natural Join

Fakultas Ilmu Komputer UI 39 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 Important Concepts:  The Set of Algebra Operations { , , , –,  } is a Complete Set of Relational Algebra Operations · Any Query Language Equivalent to These Five Operations is Called Relationally Complete All Relational Algebra Operations

Fakultas Ilmu Komputer UI 40 Fundamental Operators Selection Projection Union Set Difference Cartesian Product Relational Algebra: Summary Form:  Result>  Relation (s) Relation Additional Operators Join Intersection Quotient (Division) Union Compatibility Same Degree Corresponding Attributes Defined Over the Same Domain

Fakultas Ilmu Komputer UI 41 References Elmasri & Navathe, “Fundamental of Database Systems 3 rd ed”, Addison-Wesley, 2000

Download ppt "Relational Algebra Indra Budi Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT "

Similar presentations